mathjax + gtag

Pages

Sunday, October 19, 2014

How to post code on Blogger using Gist

The problem

How to post nice pieces of code to a blog? The desired properties are:
  1. Beauty.
  2. Language awareness.
  3. Easy to copy to the clipboard.
  4. Easy to integrate in Blogger.
  5. Easy to maintain

One possible solution

Use Gist.You must have an account on GitHub. Gists are versioned text files, so you can alter them and fix any problems on the Gist page without having to deal with your blog page.

How to integrate it into Blogger

After creating your Gist, copy the "Embed URL" to the clipboard:
The link is something like this:
<script src="https://gist.github.com/mrjimenez/bceea54fdf66a9aec7b2.js"></script>
Reserve a space to integrate the Gist in your blog post. Then choose "HTML" in the Compose/HTML radio button. Insert that code in the proper place in your page, and your're done:

/**
* Performs an exponential moving average of the specified range,
* handling missing data in a way that does not distort the average.
*
* If the spcified window size is less than the range size, an array
* is returned, so there is no need to call this function serveral
* times in a row.
*
* @param alpha The geometric progression ratio.
* @param windowSize The size of the moving average window.
* @param values The range on which we will calculate the average.
* @return The exponential moving average of the specified range.
* @customfunction
*/
function ExponentialMovingAverage(alpha, windowSize, values) {
var weights = [];
for (var i = 0; i < windowSize; i++) {
var w = Math.pow(alpha, windowSize - 1 - i);
weights[weights.length] = w;
}
var average = WeightedMovingAverage(weights, values);
return average;
}
/**
* Performs a weighted moving average of the specified range,
* handling missing data in a way that does not distort the average.
*
* @param weights An array with the weights to be applied.
* @param values The range on which we will calculate the average.
* @return The weighted moving average of the specified range.
* @customfunction
*/
function WeightedMovingAverage(weights, values) {
// If values is a scalar or weights is a scalar or
// weights has length 1, return values.
if (!values.map || !weights.map || weights.length === 1) {
return values;
}
var windowSize = weights.length;
var rows = values.length;
if (windowSize > rows) {
weights.splice(0, windowSize - rows);
windowSize = weights.length;
Logger.log("WARNING: windowSize greater than number of rows" +
" in the range, truncating the weitghts to " +
"the number of rows and using the last weights.");
}
/* If this function is called from the spreadsheet with a range,
* weights will be array of columns, lets remove this extra
* indirection. */
if (weights[0].map) {
for (var i = 0; i < weights.length; i++) {
weights[i] = weights[i][0];
}
}
var average = [];
for (var i = 0; i < rows - windowSize + 1; i++) {
var sumValues = 0;
var sumWeigths = 0;
for (var j = 0; j < windowSize; j++) {
var x = values[i + j][0];
if (typeof x === "number") {
var w = weights[j];
if (typeof w === "number") {
sumWeigths += w;
sumValues += w * x;
}
}
}
var avg = sumValues / sumWeigths;
average[average.length] = avg;
}
return average;
}

Take a look at the snapshot:


Saturday, October 18, 2014

Uploading photos to Facebook using google-chrome on linux

The problem

Uploading photos to Facebook seems to be unresponsive, I get to this screen and when I press the "Choose File" button, nothing happens:

The solution

Go to a file manager (e.g, konqueror) drag the file with the mouse and drop it over one of the buttons. The file will then be accepted:

If you want to unselect a photo, click the corresponding "Choose File" button. Apparently, its intended function is not to choose a file, it is to reset the file to "No file choosen":

After that, click "Upload Photos" and wait, because it takes some time.

Thursday, October 16, 2014

Cuda 6.5 on OpenSuSE 12.3

NVidia Drivers

Make sure you have the official NVidia drivers installed in your system:
  • Run Yast.
  • Click in "Software Repositories"
  • Click in "Add"
  • Choose "Specify URL", then "Next"
  • Repository Name: "nvidia", URL: http://download.nvidia.com/opensuse/12.3/
  • Confirm
  • Go back to Yast
  • Click in "Software Management"
  • Search for "nvidia"
  • Add the following packages: 
  1. x11-video-nvidiaG03-340.46-30.1.x86_64
  2. nvidia-gfxG03-kmp-desktop-340.46_k3.7.10_1.1-30.1.x86_64
  3. nvidia-computeG03-340.46-30.1.x86_64
  4. nvidia-settings-325.15-1.3.x86_64
  5. nvidia-glG03-340.46-30.1.x86_64
  6. nvidia-texture-tools-2.0.6-36.2.x86_64
  7. nvidia-uvm-gfxG03-kmp-desktop-340.46_k3.7.10_1.1-30.1.x86_64
Notice that the above assumes your board is supported by the G03 kernel driver and that you are using the "kernel-desktop". Make sure you choose the proper driver for your board and the kernel driver corresponding to your kernel.

CUDA Installation

Install the CUDA repository. Although the repository is for OpenSuSE 13.1, it will work perfectly with 12.3.
  • Click in "Add"
  • Choose "Specify URL", then "Next"
  • Repository Name: "cuda", URL: http://developer.download.nvidia.com/compute/cuda/repos/opensuse131/x86_64
  • Confirm
  • Go back to Yast
  • Click in "Software Management"
  • Search for "cuda"
  • Add the following packages (some of them will be automatically added): 
  1. cuda-documentation-6-5-6.5-14.x86_64
  2. cuda-cudart-6-5-6.5-14.x86_64
  3. cuda-cufft-dev-6-5-6.5-14.x86_64
  4. cuda-repo-opensuse131-6.5-14.x86_64
  5. cuda-visual-tools-6-5-6.5-14.x86_64
  6. cuda-cufft-6-5-6.5-14.x86_64
  7. cuda-npp-dev-6-5-6.5-14.x86_64
  8. cuda-curand-dev-6-5-6.5-14.x86_64
  9. cuda-license-6-5-6.5-14.x86_64
  10. cuda-runtime-6-5-6.5-14.x86_64
  11. cuda-misc-headers-6-5-6.5-14.x86_64
  12. cuda-samples-6-5-6.5-14.x86_64
  13. cuda-curand-6-5-6.5-14.x86_64
  14. cuda-toolkit-6-5-6.5-14.x86_64
  15. cuda-cublas-6-5-6.5-14.x86_64
  16. cuda-cusparse-dev-6-5-6.5-14.x86_64
  17. cuda-drivers-340.29-0.x86_64
  18. cuda-cudart-dev-6-5-6.5-14.x86_64
  19. cuda-npp-6-5-6.5-14.x86_64
  20. cuda-command-line-tools-6-5-6.5-14.x86_64
  21. cuda-cusparse-6-5-6.5-14.x86_64
  22. cuda-6.5-14.x86_64
  23. cuda-core-6-5-6.5-14.x86_64
  24. cuda-cublas-dev-6-5-6.5-14.x86_64
  25. cuda-driver-dev-6-5-6.5-14.x86_64
  26. cuda-6-5-6.5-14.x86_64
  • Click "Accept".

Testing





Usefull Links

Monday, October 6, 2014

Moving Averages Using Google Apps Scripts

Abstract

A moving average (MA) process, also known as weighted moving average (WMA) is a type of signal filtering that consists in performing a weighted average over a finite sequence of past samples of the original signal. Implementing such scheme in a worksheet is not always straightforward due to the handling of missing values. This article proposes an implementation of such filters on Google Sheets using Google App Scripts, a Javascript like language. Exponential Moving Average (EMA) filters are an important special case of WMA, so they have been implemented on top of WMA.

Keywords

Moving Average, MA, Weighted Moving Average, WMA, Exponential Moving Average, EMA, Noise Removal, Filtering, Google Sheets, Spreadsheets, Google App Scripts, Javascript.

Introduction

In previous articles [1][2], implementations of WMA and EMA have been proposed using the normal spreadsheet function infrastructure. These implementations try to be compatible with existing spreadsheet standard functions so that they can be easily ported to other spreadsheet e.g., OpenOffice.

The inconvenient in this solution is that the formulas are large and as a consequence, hard to read and maintain, making it easy to slip subtle errors. Also, the formula must be called once for each line to be calculated, and this process has a big overhead. The ideal solution should make a single call to a function that would return an array of processed data. The following implementation addresses both issues.

Implementation

/**
* Performs an exponential moving average of the specified range,
* handling missing data in a way that does not distort the average.
*
* If the spcified window size is less than the range size, an array
* is returned, so there is no need to call this function serveral
* times in a row.
*
* @param alpha The geometric progression ratio.
* @param windowSize The size of the moving average window.
* @param values The range on which we will calculate the average.
* @return The exponential moving average of the specified range.
* @customfunction
*/
function ExponentialMovingAverage(alpha, windowSize, values) {
var weights = [];
for (var i = 0; i < windowSize; i++) {
var w = Math.pow(alpha, windowSize - 1 - i);
weights[weights.length] = w;
}
var average = WeightedMovingAverage(weights, values);
return average;
}
/**
* Performs a weighted moving average of the specified range,
* handling missing data in a way that does not distort the average.
*
* @param weights An array with the weights to be applied.
* @param values The range on which we will calculate the average.
* @return The weighted moving average of the specified range.
* @customfunction
*/
function WeightedMovingAverage(weights, values) {
// If values is a scalar or weights is a scalar or
// weights has length 1, return values.
if (!values.map || !weights.map || weights.length === 1) {
return values;
}
var windowSize = weights.length;
var rows = values.length;
if (windowSize > rows) {
weights.splice(0, windowSize - rows);
windowSize = weights.length;
Logger.log("WARNING: windowSize greater than number of rows" +
" in the range, truncating the weitghts to " +
"the number of rows and using the last weights.");
}
/* If this function is called from the spreadsheet with a range,
* weights will be array of columns, lets remove this extra
* indirection. */
if (weights[0].map) {
for (var i = 0; i < weights.length; i++) {
weights[i] = weights[i][0];
}
}
var average = [];
for (var i = 0; i < rows - windowSize + 1; i++) {
var sumValues = 0;
var sumWeigths = 0;
for (var j = 0; j < windowSize; j++) {
var x = values[i + j][0];
if (typeof x === "number") {
var w = weights[j];
if (typeof w === "number") {
sumWeigths += w;
sumValues += w * x;
}
}
}
var avg = sumValues / sumWeigths;
average[average.length] = avg;
}
return average;
}

Results

The resulting spreadsheet shows a comparison of the previously posted methods with this scripted based one.

Conclusion

Two javascript functions have been developed to implement the missing data weighted moving averages of previous articles [1][2]. The results have been shown to be identical. The javascript based method has the advantage of being much cleaner to maintain, typically requiring a single cell on the spreadsheet.