• R/O
  • HTTP
  • SSH
  • HTTPS

cpp: Commit

psychlops cpp


Commit MetaInfo

Revision0d6f30dda998b99119681bf9015e1f34be9a4855 (tree)
Time2015-07-15 23:57:54
Authorhskwk <hosokawa.kenchi@gmai...>
Commiterhskwk

Log Message

test

Change Summary

Incremental Difference

--- a/psychlops/extension/standard/fft/psychlops_FFTW_bridge.cpp
+++ b/psychlops/extension/standard/fft/psychlops_FFTW_bridge.cpp
@@ -22,6 +22,127 @@ namespace Psychlops {
2222
2323
2424
25+ FFT1::FFT1()
26+ {
27+ construct_default();
28+ }
29+ FFT1::FFT1(int wid)
30+ {
31+ construct_default();
32+ set(wid);
33+ }
34+ FFT1::FFT1(const Matrix &source)
35+ {
36+ construct_default();
37+ set(source);
38+ }
39+ FFT1::~FFT1()
40+ {
41+ release();
42+ }
43+ void FFT1::construct_default()
44+ {
45+ width_=0;
46+ img_spc=0; frq_spc=0;
47+ }
48+ void FFT1::release()
49+ {
50+ if(img_spc!=0) { free(img_spc); img_spc = 0; }
51+ if(frq_spc!=0) { free(frq_spc); frq_spc = 0; }
52+ width_=0;
53+ }
54+ void FFT1::set(int wid)
55+ {
56+ if(img_spc==0 || frq_spc==0 || (width_ != wid) ) {
57+ release();
58+ img_spc = (fftw_complex*)malloc(sizeof(fftw_complex) * wid);
59+ frq_spc = (fftw_complex*)malloc(sizeof(fftw_complex) * wid);
60+ }
61+ width_ = wid;
62+ x_zero = wid + 1;
63+ left = (wid)/2;
64+ }
65+ void FFT1::set(const Matrix &source)
66+ {
67+ if(source.getRows()==1) {
68+ set(source.getCols());
69+ for(int x=0; x<width_; x++) {
70+ img_spc[x][REAL] = source(1, x+1);
71+ img_spc[x][IMAGINARY] = 0.0;
72+ }
73+ }
74+ }
75+
76+ double FFT1::getDC()
77+ {
78+ return sqrt(frq_spc[0][REAL]*frq_spc[0][REAL]+frq_spc[0][IMAGINARY]*frq_spc[0][IMAGINARY]);
79+ }
80+ double FFT1::setDC(double l)
81+ {
82+ frq_spc[0][REAL] = l;
83+ frq_spc[0][IMAGINARY] = 0;
84+ return l;
85+ }
86+
87+
88+ void FFT1::fft()
89+ {
90+ fftw_plan plan = fftw_plan_dft_1d(width_, img_spc, frq_spc, FFTW_FORWARD, FFTW_ESTIMATE);
91+ fftw_execute(plan);
92+ normalizeFFT();
93+ }
94+ void FFT1::ifft()
95+ {
96+ fftw_plan plan = fftw_plan_dft_1d(width_, frq_spc, img_spc, FFTW_BACKWARD, FFTW_ESTIMATE);
97+ fftw_execute(plan);
98+ }
99+ void FFT1::normalizeFFT()
100+ {
101+ double num = width_;
102+ for (int i=0; i<num; i++) {
103+ frq_spc[i][REAL] /= num;
104+ frq_spc[i][IMAGINARY] /= num;
105+ }
106+ }
107+ void FFT1::getImage(Matrix &target)
108+ {
109+ double rl, im;
110+ int h, v;
111+ if( (target.getCols()!=width_)) {
112+ target.release();
113+ target.set(1, width_);
114+ }
115+ for (int x=0; x<width_; x++) {
116+ rl = img_spc[x][REAL];
117+ im = img_spc[x][IMAGINARY];
118+ target(1, x+1) = sqrt(rl*rl+im*im);
119+ }
120+ }
121+ void FFT1::getSpectrum(Matrix &target, double gamma)
122+ {
123+ const int right = width_%2==0 ? left : left+1;
124+ double rl, im;
125+ Color col;
126+ if( (target.getCols()!=width_) ) {
127+ target.release();
128+ target.set(1, width_);
129+ }
130+ int h, v = 1;
131+ for (int x=0; x<right; x++) {
132+ h = x+left + 1;
133+ rl = frq_spc[x][REAL];
134+ im = frq_spc[x][IMAGINARY];
135+ target(v, h) = pow( sqrt(rl*rl+im*im), gamma );
136+ }
137+ for (int x=right; x<width_; x++) {
138+ h = x-right + 1;
139+ rl = frq_spc[x][REAL];
140+ im = frq_spc[x][IMAGINARY];
141+ target(v, h) = pow( sqrt(rl*rl+im*im), gamma );
142+ }
143+ }
144+
145+
25146
26147 FFT2::FFT2()
27148 {
--- a/psychlops/extension/standard/fft/psychlops_FFTW_bridge.h
+++ b/psychlops/extension/standard/fft/psychlops_FFTW_bridge.h
@@ -20,6 +20,40 @@ namespace Psychlops {
2020 double cumulativeLog2NormalDistibution(double log_x, double octave_mu, double octave_sigma);
2121
2222
23+ class FFT1 {
24+ protected:
25+ int width_;
26+ int left, x_zero;
27+ fftw_complex *img_spc;
28+ fftw_complex *frq_spc;
29+ void construct_default();
30+ public:
31+
32+ // Initialize
33+ FFT1();
34+ FFT1(int width);
35+ FFT1(const Matrix &source);
36+ ~FFT1();
37+ void release();
38+ void set(int wid);
39+ void set(const Matrix &source);
40+
41+ // Accesser to elements
42+ double getDC();
43+ double setDC(double l);
44+
45+ // Core FFT Execution
46+ void fft();
47+ void ifft();
48+ void normalizeFFT();
49+
50+ void getImage(Matrix &absolute);
51+ void getSpectrum(Matrix &absolute, double gamma = 1.0);
52+ //void getImage(Matrix &reali, Matrix &imagi);
53+ //void getSpectrum(Matrix &reali, Matrix &imagi);
54+
55+ };
56+
2357 class FFT2 {
2458 protected:
2559 int width_, height_;
--- a/win32gl/test/Psychlops_win32cblibtest.cbp
+++ b/win32gl/test/Psychlops_win32cblibtest.cbp
@@ -180,19 +180,19 @@
180180 <Option compile="0" />
181181 <Option link="0" />
182182 </Unit>
183- <Unit filename="basickFlickerTest.cpp" />
184- <Unit filename="bugfix.cpp">
183+ <Unit filename="basickFlickerTest.cpp">
185184 <Option compile="0" />
186185 <Option link="0" />
187186 </Unit>
188- <Unit filename="compilenew.cpp">
187+ <Unit filename="bugfix.cpp">
189188 <Option compile="0" />
190189 <Option link="0" />
191190 </Unit>
192- <Unit filename="fftfirm.cpp">
191+ <Unit filename="compilenew.cpp">
193192 <Option compile="0" />
194193 <Option link="0" />
195194 </Unit>
195+ <Unit filename="fftfirm.cpp" />
196196 <Unit filename="inputTest.cpp">
197197 <Option compile="0" />
198198 <Option link="0" />
--- a/win32gl/test/fftfirm.cpp
+++ b/win32gl/test/fftfirm.cpp
@@ -1,6 +1,45 @@
11 #include <psychlops.h>
22 using namespace Psychlops;
33
4+
5+void psychlops_main() {
6+ Canvas cnvs(1024, 768, Canvas::window);
7+
8+ FFT1 fftworkspace;
9+ Matrix source_mat, filtered_mat, mat;
10+ source_mat.load("voltage_spec_vaio_ie.csv");
11+ source_mat.transpose();
12+
13+ //fftworkspace.set(64,64);
14+ //fftworkspace.makeNoise();
15+ fftworkspace.set(source_mat);
16+ fftworkspace.fft();
17+ fftworkspace.getSpectrum(filtered_mat, 1.0);
18+
19+ int q = filtered_mat.getCols() / 4;
20+ Interval col;
21+ mat = filtered_mat(1, q*2+3<=col<=q*3);
22+ mat.transpose();
23+ mat.save("voltage_spec.csv");
24+
25+ double mean = 0.0;
26+ for(int i=0; i<source_mat.getCols(); i++) {
27+ mean += source_mat(1, i+1);
28+ }
29+ mean /= source_mat.getCols();
30+ int counter = 0;
31+ for(int i=1; i<source_mat.getCols()-3; i++) {
32+ if( source_mat(1, i+1)<mean
33+ && source_mat(1, i+2)<mean
34+ && source_mat(1, i+3)>mean
35+ && source_mat(1, i+4)>mean
36+ ) { counter++; }
37+ }
38+ std::cout << counter << std::endl;
39+}
40+
41+
42+/*
443 const int S = 128;
544
645 void psychlops_main() {
@@ -42,3 +81,4 @@ const int S = 128;
4281
4382 }
4483
84+*/
Show on old repository browser