Created
March 31, 2015 15:45
-
-
Save pavanky/167a4f60089d0dcd6d3d to your computer and use it in GitHub Desktop.
Moving average with arrayfire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************* | |
* Copyright (c) 2014, ArrayFire | |
* All rights reserved. | |
* | |
* This file is distributed under 3-clause BSD license. | |
* The complete license agreement can be obtained at: | |
* http://arrayfire.com/licenses/BSD-3-Clause | |
********************************************************/ | |
#include <iostream> | |
#include <limits> | |
#include <arrayfire.h> | |
#include <vector> | |
using namespace std; | |
using namespace af; | |
array moving_average(const array &input, const vector<int> w, const int max_size) | |
{ | |
// Create weight matrix of all zeros | |
array weights = constant(0, max_size, w.size()); | |
// Can not use gfor here | |
for (int ii = 0; ii < w.size(); ii++) { | |
int w_len = w[ii]; | |
// Calculate the norm factor for current window length | |
float norm = 1.0 / (float)(w_len); | |
// Only make the first "len" elements equal to the norm | |
weights(seq(1, w_len), ii) = constant(norm, w_len, 1); | |
} | |
// Calculate the moving averages in parallel | |
// This is the most compute intensive part | |
array res = convolve1(input, weights); | |
return res; | |
} | |
int main() | |
{ | |
af::deviceset(0); | |
af::info(); | |
array a = range(10, 1); | |
// 3 window sizes | |
std::vector<int> w(3); | |
// Set window sizes | |
w[0] = 2; | |
w[1] = 3; | |
w[2] = 4; | |
int max_size = 5; | |
array b = moving_average(a, w, max_size); | |
af_print(a); | |
af_print(b); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./examples/helloworld/helloworld_cuda | |
ArrayFire v3.0.beta (CUDA, 64-bit Linux, build 102bf9b) | |
Platform: CUDA Toolkit 7, Driver: 346.47 | |
[0] GeForce GTX 690, 2048 MB, CUDA Compute 3.0 | |
-1- GeForce GTX 690, 2048 MB, CUDA Compute 3.0 | |
a [10 1 1 1] | |
0.0000 | |
1.0000 | |
2.0000 | |
3.0000 | |
4.0000 | |
5.0000 | |
6.0000 | |
7.0000 | |
8.0000 | |
9.0000 | |
b [10 3 1 1] | |
0.5000 0.3333 0.2500 | |
1.5000 1.0000 0.7500 | |
2.5000 2.0000 1.5000 | |
3.5000 3.0000 2.5000 | |
4.5000 4.0000 3.5000 | |
5.5000 5.0000 4.5000 | |
6.5000 6.0000 5.5000 | |
7.5000 7.0000 6.5000 | |
8.5000 8.0000 7.5000 | |
4.5000 5.6667 6.0000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment