JASPL  0.2
Just Another Signal Processing Library
jfilter_unit_test.h
1 #ifndef JFILTER_UNIT_TEST_H
2 #define JFILTER_UNIT_TEST_H
3 
4 //Header for this file
5 //
6 //C System-Headers
7 #include <math.h>
8 //C++ System headers
9 #include <iostream>
10 #include <chrono>
11 //OpenCL Headers
12 //
13 //Boost Headers
14 //
15 //Project specific headers
16 #include "jVector/jvector.h"
17 #include "jFFT/jfft.h"
18 #include "jPlot/jplot.h"
19 #include "jFilter/jlinearfilter.h"
20 #include "jTypeTraits/jtypetraits.h"
21 
22 namespace jaspl {
23 
24 template <typename T>
26 
27  public:
28  void CheckFilterCPU( uint num_points ) {
29 
30  uint kernel_radius = static_cast<uint>( floor( num_points/100 ) );
31 
32  uint kernel_bytes = kernel_radius*2*sizeof( T );
33 
34  if( kernel_bytes > CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE ) {
35  kernel_radius = static_cast<uint>( CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE/( 2 * sizeof(T) ) );
36  }
37 
38  JVector<T> signal = SineSignal( num_points );
39 
40  std::string mesg = "CPU Linear Convolution Test:";
41  mesg += "\nSignal size:";
42  mesg += boost::lexical_cast<std::string>( num_points );
43  mesg += "\nKernel size:";
44  mesg += boost::lexical_cast<std::string>( kernel_radius );
45 
46  std::cout << mesg << std::endl;
47 
48  TestCPUConvolve( signal, kernel_radius );
49 
50  }
51 
52  void CheckFilterGPU( uint num_points ) {
53 
54  int kernel_radius = static_cast<int>( floor( num_points/100 ) );
55 
56  uint kernel_bytes = kernel_radius*2*sizeof( T );
57 
58  if( kernel_bytes > CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE ) {
59  kernel_radius = static_cast<uint>( CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE/( 2 * sizeof(T) ) );
60  }
61 
62  JVector<T> signal = SineSignal( num_points );
63 
64  std::string mesg = "GPU Linear Convolution Test:";
65  mesg += "\nSignal size:";
66  mesg += boost::lexical_cast<std::string>( num_points );
67  mesg += "\nKernel size:";
68  mesg += boost::lexical_cast<std::string>( kernel_radius );
69 
70  std::cout << mesg << std::endl;
71 
72  TestGPUConvolve( signal, kernel_radius );
73  }
74 
75  private:
76 
77  uint num_elem_per_MB ( uint MB ) {
78  return static_cast<uint> (MB/4*1e5);
79  }
80 
81  inline double gaussian(double x, double sigma) {
82  return 1/(sqrt(M_PI_2)*sigma)*exp( -0.5 *pow(x/sigma,2.0));
83  }
84 
85  JVector<T> HeavisideThetaSignal( uint N ) {
86 
87  JVector<T> theta_vect ( N );
88 
89  for ( uint i = 0; i < N/2 ; i++ ) {
90 
91  theta_vect[i] = 0.0;
92  }
93 
94 
95  for ( uint i = N/2; i < N ; i++ ) {
96 
97  theta_vect[i] = 1.0;
98  }
99 
100  return theta_vect;
101  }
102 
103  JVector<T> SineSignal( uint N ) {
104 
105  JVector<T> sin_vect ( N );
106 
107  for ( uint i = 0; i < N ; i++ ) {
108 
109  sin_vect.push_back( sinf( 2*i *2*M_PI/N) + sinf( 25*i*2*M_PI/N) + sinf( 100*i*2*M_PI/N ) );
110  }
111 
112  return sin_vect;
113  }
114 
115 
116  void TestCPUConvolve ( jaspl::JVector<T>&vec, uint kernel_radius ) {
117 
118  plot( vec, "Original", 500 );
119 
120  jaspl::JVector<T> kernel;
121 
122  int r = kernel_radius;
123  double sigma = static_cast<double>(r)/2.0;
124 
125  for( int i = -r; i<= r ; i ++) {
126  kernel.push_back(gaussian(i,sigma));
127  }
128 
129  kernel.Normalize();
130 
131  auto start_cpu = std::chrono::high_resolution_clock::now();
132 
133  auto convolved_vect = jaspl::JLinearConvolve( vec, kernel );
134 
135  plot( convolved_vect, "Convolved (CPU)", 500 );
136 
137  auto end_cpu = std::chrono::high_resolution_clock::now();
138  std::chrono::duration<double, std::milli> cpu_ms = end_cpu - start_cpu;
139  auto time_taken_cpu = cpu_ms.count();
140 
141  std::cout<<"CPU took "<<time_taken_cpu<<" ms."<<std::endl;
142 
143  }
144 
145  void TestGPUConvolve ( jaspl::JVector<T>&vec, int kernel_radius ) {
146 
147  plot( vec, "Original", 500 );
148 
149 
150  jaspl::JVector<T> kernel;
151 
152  int r = kernel_radius;
153  double sigma = static_cast<double>(r)/2.0;
154 
155  for( int i = -r; i<= r ; i ++) {
156  kernel.push_back(gaussian(i,sigma));
157  }
158 
159  kernel.Normalize();
160 
161  auto start_gpu = std::chrono::high_resolution_clock::now();
162 
163  auto end_gpu = std::chrono::high_resolution_clock::now();
164  std::chrono::duration<double, std::milli> gpu_ms = end_gpu - start_gpu;
165  auto time_taken_gpu = gpu_ms.count();
166 
167  std::cout<<"GPU took "<<time_taken_gpu<<" ms."<<std::endl;
168 
169  }
170 };
171 
172 }
173 
174 #endif // JFILTER_UNIT_TEST_H