Upload
nguyenduong
View
222
Download
0
Embed Size (px)
Citation preview
ECE449
1D and 2D signals
Project
Jovan Brankov
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________2
CONTENTS
INTRODUCTION 2
TEST DRIVE RESULTS 4
TEST DRIVE PROGRAM 6
DEVELOPMENT TEST PROGRAM RESULTS 8
DEVELOPMENT TEST PROGRAM 13
COMPLEX CLASS 17Complex.hpp 17Complex.cpp 19
SIGNAL_1D TEMPLATE CLASSSignal_1D.hpp 20
SIGNAL_2D TEMPLATE CLASSSignal_2D.hpp 28
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________3
The idea was to model 1D and 2D signals (or vector and matrix class) for all possible types of elements (int, long,float, double and Complex).
Signal_1D template class:
unimplemented methodsConstructors:(it can be Row or Column)
• from the same class,• from given constant and size,• from given size,• from file
destuctor yesAccessory functions : conj() Conjugate
max() maximummin() minimumdot() vector product between Row and
Column or vice versanorm() length of the vector(i) element at i positionsize size of the 2D signal
Primitive arithmetic memberoperators
+=,-=,*=,/=,-,+,! for corresponding elements
Relational member operators: ==,!= for corresponding elementsAdditional arithmetic member +,-,*,/ for corresponding elementsAdditional (non-member) fft 1D-FFT
T transpose from Row to Column
and vice versareal real partimag imaginary parttypeof return type of signalfwrite write the object to filefread read the object from file
Note: The Signal_1D class can distinguish Row and Column class.
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________4
Signal_2D template class:
unimplemented methodsConstructors: • from the same class,
• from given constant and size,• from given size,• from file
destuctor yesAccessory functions : conj() - Conjugate
max() – maximummin() – minimum(i,j) – element at i,j position(i) – ith column (Signal_1D class)size – size of the 2D signalT - transpose
det matrix determinateinv inverse matrix
Primitive arithmetic memberoperators
+=,-=,*=,/=,-,+,! for corresponding elements
Relational member operators: ==,!= Rational operatorsAdditional arithmetic member +,-,*,/ for corresponding elements dot matrix mul. Additional (non-member) fft 1D-FFT on each column
fft2 2D-FFT on each columnreal real partimag imaginar parttypeof return type of signalfwrite write the object to filefread read the object from file
Note: The Signal_2D class consists of N Column 1D signals that makes the allocated memory required biggerby N-1 integers. However, because that we can consider each Column of the 2D signal as a 1D signal or the 2Dsignal is a set of 1D signals, which is a case often in practice. That will make some of the function much faster andeasier to implement. (Like fft, fft2).
Note: It can appear that destructor for Signal_2D is wrong but, I have run this simple program and there wasno memory leaking.
// Test#include "s_1d.hpp"#include "s_2d.hpp"#include <assert.h>#include <iostream.h>#include <stdlib.h>int main(){ int M,N; M=2000;N=M;cout<<endl;cout<<"Number of kb in signal: "<<sizeof(double)*M*N*2/1024; cout<<endl;
for(int i=1;i<100;++i) {{Signal_2D<Complex> w9(Complex(rand(),rand()),M,N);fft(w9,1);}
cerr<<i<<"\n";}int x;cin>>x;return 0;}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________5
TEST drive program:a= [ (0,0) (1,1) (2,2) (3,3) ]
Type of a:Complex_Rowdimension: [ 4 ]
b=tran(a) [ (0,0) (1,1) (2,2) (3,3)]
Type of b:Complex_Columndimension: [ 4 ]
a*b= (0,28)
c=b*a= [[ (0,0) (0,0) (0,0) (0,0) ][ (0,0) (0,2) (0,4) (0,6) ][ (0,0) (0,4) (0,8) (0,12) ][ (0,0) (0,6) (0,12) (0,18) ]]
Real_Part(c)=[[ 0 0 0 0 ][ 0 0 0 0 ][ 0 0 0 0 ][ 0 0 0 0 ]]
Imag_Part(c)=[[ 0 0 0 0 ][ 0 2 4 6 ][ 0 4 8 12 ][ 0 6 12 18 ]]
Type of c:Complexdimension: [ 4 4 ]
d=fft(c)=[[ (0,0) (0,12) (0,24) (0,36) ][ (0,0) (-4,-4) (-8,-8) (-12,-12) ][ (0,0) (0,-4) (0,-8) (0,-12) ][ (0,0) (4,-4) (8,-8) (12,-12) ]]ifft(d)=[[ (0,0) (0,0) (0,0) (0,0) ][ (0,0) (0,2) (0,4) (0,6) ][ (0,0) (0,4) (0,8) (0,12) ][ (0,0) (0,6) (0,12) (0,18) ]]Conj(d)=[[ (0,0) (0,-12) (0,-24) (0,-36) ][ (0,0) (-4,4) (-8,8) (-12,12) ][ (0,0) (0,4) (0,8) (0,12) ][ (0,0) (4,4) (8,8) (12,12) ]]
Writing d matrix in a file...Constructor a variable e from file...
e= [[ (0,0) (0,12) (0,24) (0,36) ][ (0,0) (-4,-4) (-8,-8) (-12,-12) ][ (0,0) (0,-4) (0,-8) (0,-12) ][ (0,0) (4,-4) (8,-8) (12,-12) ]]
Type of e:Complexdimension: [ 4 4 ]
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________6
e*d= [[ (0,0) (-144,0) (-576,0) (-1296,0) ][ (0,0) (6.99382e-007,32) (2.79753e-006,128) (1.77385e-005,288) ][ (0,0) (-16,0) (-64,0) (-144,0) ][ (0,0) (-6.99382e-007,-32)(-2.79753e-006,-128) (-1.77385e-005,-288) ]]
f=real(e)= [[ 0 0 0 0 ][ 0 -4 -8 -12 ][ 0 0 0 0 ][ 0 4 8 12 ]]
Type of f:doubledimension: [ 4 4 ]
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________7
______________________________________________________________________________________
file: Test.cpp______________________________________________________________________________________
#include "s_2D.hpp"#include "s_1D.hpp"//#include <assert.h>#include <iostream.h>#include <stdlib.h>
#define Row ’R’#define Col ’C’
int main(){Signal_1D<Complex,Row> a(4);for (int i=0;i<4;++i)
a(i)=Complex(i,i);
cout<<"a= "<<a<<endl;cout<<"Type of a:"<<typeof(a)<<endl;cout<<"dimension: "<<a.size()<<endl;
Signal_1D<Complex,Col> b(T(a));
cout<<"b=tran(a) "<<b<<endl;cout<<"Type of b:"<<typeof(b)<<endl;cout<<"dimension: "<<b.size()<<endl;
cout<<"a*b= "<<a*b<<endl;
Signal_2D<Complex> c;c=b*a;cout<<"c=b*a= "<<c<<endl;cout<<"Real_Part(c)="<<real(c)<<endl;cout<<"Imag_Part(c)="<<imag(c)<<endl;cout<<"Type of c:"<<typeof(c)<<endl;cout<<"dimension: "<<c.size()<<endl;
Signal_2D<Complex> d(fft(c,1));cout<<"d=fft(c)="<<d;cout<<"ifft(d)="<<fft(d,-1);cout<<"Conj(d)="<<d.conj()<<endl;cout<<"Writing d matrix in a file..."<<endl;fwrite(d,"Dmatrix.dat");cout<<"Constructor a variable e from file..."<<endl<<endl;
Signal_2D<Complex> e("Dmatrix.dat");
cout<<"e= "<<e<<endl;cout<<"Type of e:"<<typeof(e)<<endl;cout<<"dimension: "<<e.size()<<endl;
cout<<"e*d= "<<e*d<<endl;
Signal_2D<double> f(real(e));
cout<<"f=real(e)= "<<f<<endl;cout<<"Type of f:"<<typeof(f)<<endl;cout<<"dimension: "<<f.size()<<endl;
return 0;}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________8
_Development test Program_results(1,2)0element(15724,19169)(1,7)elementsize[ 5 ]
[ (14604,32391) (153,3902) (12382,292) (18716,17421) (19895,19718)]
constructorobican i conj[ (18467,41) (26500,6334) (1,7) (29358,11478) (24464,26962) ]
[ (18467,-41) (26500,-6334) (1,-7) (29358,-11478) (24464,-26962) ]
uporedi00uporediow conj1[ (-18467,-41) (-26500,-6334) (-1,-7) (-29358,-11478) (-24464,-26962) ]
[ (-18467,41) (-26500,6334) (-1,7) (-29358,11478) (-24464,26962) ]
double element[ 3851.61 15362.6 10444.7 8158.6 1321.58]
[ 19912 25667 26299 17035 9894 28703 23811 31322]int element[ 19912 25667 26299 17035 9894 28703 23811 31322]
Dot (1.25507e+013,3.98382e+013)Norm 67133.4Dot (1.25507e+013,3.98382e+013)Norm 1.84703e+009[ 7 ]
[ 19912 25667 26299 17035 9894 28703 23811 31322]
[ (28145,5705) (16827,23281) (491,9961) (11942,2995) (5436,4827) ]
gen obris[[ 3 3 3 3 3 3 3 3 ][ 3 3 3 3 3 3 3 3 ][ 3 3 3 3 3 3 3 3 ][ 3 3 3 3 3 3 3 3 ][ 3 3 3 3 3 3 3 3 ][ 3 3 3 3 3 3 3 3 ][ 3 3 3 3 3 3 3 3 ][ 3 3 3 3 3 3 3 3 ]][[ 0 0 3 3 6 6 9 9 ][ 3 3 6 6 9 9 12 12 ][ 6 6 9 9 12 12 15 15 ][ 9 9 12 12 15 15 18 18 ][ 12 12 15 15 18 18 21 21 ][ 15 15 18 18 21 21 24 24 ][ 18 18 21 21 24 24 27 27 ][ 21 21 24 24 27 27 30 30 ]][
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________9
[ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ]][ 8 8 ][ 7 ][ 1 1 1 1 1 1 1] Col[[ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ][ 3.12 3.12 3.12 3.12 3.12 3.12 3.12 3.12 ]]original[[ 0 0 1 1 2 2 3 3 ][ 1 1 2 2 3 3 4 4 ][ 2 2 3 3 4 4 5 5 ][ 3 3 4 4 5 5 6 6 ][ 4 4 5 5 6 6 7 7 ][ 5 5 6 6 7 7 8 8 ][ 6 6 7 7 8 8 9 9 ][ 7 7 8 8 9 9 10 10 ]]original[[ 1.#INF 1.#INF 3 3 1.5 1.5 1 1 ][ 3 3 1.5 1.5 1 1 0.75 0.75 ][ 1.5 1.5 1 1 0.75 0.75 0.6 0.6 ][ 1 1 0.75 0.75 0.6 0.6 0.5 0.5 ][ 0.75 0.75 0.6 0.6 0.5 0.5 0.428571 0.428571 ][ 0.6 0.6 0.5 0.5 0.428571 0.428571 0.375 0.375 ][ 0.5 0.5 0.428571 0.428571 0.375 0.375 0.333333 0.333333 ][ 0.428571 0.428571 0.375 0.375 0.333333 0.333333 0.3 0.3 ]]
[ 0.666667 0.666667 0.666667 0.666667 0.666667 0.666667 0.666667]chanceg[[ 0 0 1 1 2 2 3 3 ][ 1 1 2 2 3 3 4 4 ][ 2 2 3 3 4 4 5 5 ][ 3 3 4 4 5 5 6 6 ][ 4 4 5 5 6 6 7 7 ][ 5 5 6 6 7 7 8 8 ][ 6 6 7 7 8 8 9 9 ][ 7 7 8 8 9 9 10 10 ]]
[[ 0 1 2 3 4 5 6 7 ][ 0 1 2 3 4 5 6 7 ][ 1 2 3 4 5 6 7 8 ][ 1 2 3 4 5 6 7 8 ][ 2 3 4 5 6 7 8 9 ][ 2 3 4 5 6 7 8 9 ][ 3 4 5 6 7 8 9 10 ][ 3 4 5 6 7 8 9 10 ]][ 0 1 2 3 4 5 6][ 0 1 2 3 4 5 6 ][[ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ]][ 0 1 2 3 4 5
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________10
6][ (14604,-32391) (153,-3902) (12382,-292) (18716,-17421) (19895,-19718)]0(153,3902)6(14604,32391)0(4,3)10(4,3)[ 8 8 ]10[ (0,0) (2,0) (4,0) (6,0) (8,0) (10,0) (12,0) (14,0)][ (0,0) (1,0) (2,0) (3,0) (4,0) (5,0) (6,0) (7,0) ][ (28,0) (-4,9.65685) (-4,4) (-4,1.65685) (-4,0) (-4,-1.65685) (-4,-4) (-4,-9.65685)][ (3.12,1) (3.12,1) (3.12,1) (3.12,1) (3.12,1) (3.12,1) (3.12,1) (3.12,1) (3.12,1)]2_D[[ (0,0) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (1,1) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (2,2) (4,-3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (3,3) (4,-3) (4,-3) (4,-3) (4,-3) ][ (4,-3) (4,-3) (4,-3) (4,-3) (4,4) (4,-3) (4,-3) (4,-3) ][ (4,3) (4,3) (4,3) (4,3) (4,3) (5,5) (4,3) (4,3) ][ (4,3) (4,3) (4,3) (4,3) (4,3) (4,3) (6,6) (4,3) ][ (4,3) (4,3) (4,3) (4,3) (4,3) (4,3) (4,3) (7,7) ]][ (4,-3) (4,-3) (4,-3) (3,3) (4,-3) (4,-3) (4,-3) (4,-3) ][[ 252 0.828427 -8 -3.17157 -4 -4.82843 -1.74846e-007 -8.82843 ][ -115.054 -8 -3.17157 -4 -4.82843 -1.01114e-006 -8.82843 -4 ][ -8 -3.17157 -4 -4.82843 -1.74846e-007 -8.82843 -4 0.828428 ][ -23.0538 -4 -4.82843 -1.68549e-006 -8.82843 -4 0.828428 -8 ][ -4 -4.82843 -1.74846e-007 -8.82843 -4 0.828427 -8 -3.17157 ][ 15.0538 -1.01114e-006 -8.82843 -4 0.828427 -8 -3.17157 -4 ][ -1.74846e-007 -8.82843 -4 0.828428 -8 -3.17157 -4 -4.82843 ][ 107.054 -4 0.828428 -8 -3.17157 -4 -4.82843 -1.68549e-006 ]]0 seconds[[ (1339.14,4405.14)(656.571,1574.29) (3906.86,3431.57) (2809.71,3314.14) (1183,3497.71) (7.57143,676.286) (3774,285.571) (985.714,3991.14)
][ (2589.57,541.143)(532.571,66.7143) (3521.14,2127.57) (2543.86,3211.86) (2044.29,345.857) (3259,945.286) (2044.14,1359.14) (2705,1088) ][ (2942.86,2493) (2359.86,749.857) (3256.86,4508) (889.143,4329) (834.857,1572.57) (2141.29,4658.43) (456.429,4671.71) (441.857,2926.43)
][ (4360.43,2049) (4187.71,226.714) (1064,1357.57) (1922.57,3600) (2940,945.429) (2114,2828) (2798.43,2183) (4001.29,2971.14) ][ (2924.57,3879.57)(2648.29,3374.57) (862.571,1756) (2598.57,3454.14) (1136.86,4236.71) (2830.71,884.429) (2736.57,3269.71) (2314.57,1644.43)
][ (3467.43,376.286)(2904,2865) (3766,3235.14) (2696.43,698) (4267,4061.86) (3406.29,2877.43) (3125.86,202.286) (1474.57,4571.14) ][ (1431.57,2664.43)(508.143,814.143) (3984.57,4068) (725,3484.14) (371.429,1530.29) (3000.43,358.571) (2551.57,3838.43) (1914.43,2098.29)
][ (2179.29,1398.43)(714.571,2346.14) (3454.57,1512.14) (3869.71,1469.29) (4088.14,4489.43) (1404.57,3393.86) (595.571,4418.86) (3674.43,307.714)
]]original size: [ 8 8 ]
from file size: [ 8 8 ][[ (1339.14,4405.14)(656.571,1574.29) (3906.86,3431.57) (2809.71,3314.14) (1183,3497.71) (7.57143,676.286) (3774,285.571) (985.714,3991.14)
][ (2589.57,541.143)(532.571,66.7143) (3521.14,2127.57) (2543.86,3211.86) (2044.29,345.857) (3259,945.286) (2044.14,1359.14) (2705,1088) ][ (2942.86,2493) (2359.86,749.857) (3256.86,4508) (889.143,4329) (834.857,1572.57) (2141.29,4658.43) (456.429,4671.71) (441.857,2926.43)
][ (4360.43,2049) (4187.71,226.714) (1064,1357.57) (1922.57,3600) (2940,945.429) (2114,2828) (2798.43,2183) (4001.29,2971.14) ][ (2924.57,3879.57)(2648.29,3374.57) (862.571,1756) (2598.57,3454.14) (1136.86,4236.71) (2830.71,884.429) (2736.57,3269.71) (2314.57,1644.43)
][ (3467.43,376.286)(2904,2865) (3766,3235.14) (2696.43,698) (4267,4061.86) (3406.29,2877.43) (3125.86,202.286) (1474.57,4571.14) ][ (1431.57,2664.43)(508.143,814.143) (3984.57,4068) (725,3484.14) (371.429,1530.29) (3000.43,358.571) (2551.57,3838.43) (1914.43,2098.29)
][ (2179.29,1398.43)(714.571,2346.14) (3454.57,1512.14) (3869.71,1469.29) (4088.14,4489.43) (1404.57,3393.86) (595.571,4418.86) (3674.43,307.714)
]]ComplexComplex_ColumnComplex_Row
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________11
(3256.86,4508) 3256.86[[ 2455.57 2853.71 4475.57 338.286 4098.86 3060.71 1507.86 490.571 ][ 2364.14 1063 1358.86 4306.43 2580 3102.57 536.143 2305.57 ][ 1774.71 2325.57 3713.71 2383.86 1789.86 3221.29 2491 2838 ][ 1849.86 27.5714 3313.57 471 2916.57 4040.86 2300.71 3498.29 ][ 2326 1779.29 3676.29 2587.71 1671.57 4473.71 2953 826.571 ][ 1751.86 616.143 3479.29 4455 2864.71 130.286 1544 261.714 ][ 2992.14 616.143 3965.14 4045.86 2794 3378 3997.43 68.7143 ][ 592 3313.71 2888.86 1018.43 308.714 790.714 2921.43 1596.14 ]]original size: [ 8 8 ]
from file size: [ 8 8 ][[ 2455.57 2853.71 4475.57 338.286 4098.86 3060.71 1507.86 490.571 ][ 2364.14 1063 1358.86 4306.43 2580 3102.57 536.143 2305.57 ][ 1774.71 2325.57 3713.71 2383.86 1789.86 3221.29 2491 2838 ][ 1849.86 27.5714 3313.57 471 2916.57 4040.86 2300.71 3498.29 ][ 2326 1779.29 3676.29 2587.71 1671.57 4473.71 2953 826.571 ][ 1751.86 616.143 3479.29 4455 2864.71 130.286 1544 261.714 ][ 2992.14 616.143 3965.14 4045.86 2794 3378 3997.43 68.7143 ][ 592 3313.71 2888.86 1018.43 308.714 790.714 2921.43 1596.14 ]]doubledouble_Columndouble_Row[[ 10466 12044 21659 26292 26439 17253 20024 26154 ][ 29510 4745 20649 13186 8313 4474 28022 2168 ][ 14018 18787 9905 17958 7391 10202 3625 26477 ][ 4414 9314 25824 29334 25874 24372 20159 11833 ][ 28070 7487 28297 7518 8177 17773 32270 1763 ][ 2668 17192 13985 3102 8480 29213 7627 4802 ][ 4099 30527 2625 1543 1924 11023 29972 13061 ][ 14181 31003 27432 17505 27593 22725 13031 8492 ]]original size: [ 8 8 ]
[[ 10466 12044 21659 26292 26439 17253 20024 26154 ][ 29510 4745 20649 13186 8313 4474 28022 2168 ][ 14018 18787 9905 17958 7391 10202 3625 26477 ][ 4414 9314 25824 29334 25874 24372 20159 11833 ][ 28070 7487 28297 7518 8177 17773 32270 1763 ][ 2668 17192 13985 3102 8480 29213 7627 4802 ][ 4099 30527 2625 1543 1924 11023 29972 13061 ][ 14181 31003 27432 17505 27593 22725 13031 8492 ]]
from file size: [ 8 8 ][[ 10466 12044 21659 26292 26439 17253 20024 26154 ][ 29510 4745 20649 13186 8313 4474 28022 2168 ][ 14018 18787 9905 17958 7391 10202 3625 26477 ][ 4414 9314 25824 29334 25874 24372 20159 11833 ][ 28070 7487 28297 7518 8177 17773 32270 1763 ][ 2668 17192 13985 3102 8480 29213 7627 4802 ][ 4099 30527 2625 1543 1924 11023 29972 13061 ][ 14181 31003 27432 17505 27593 22725 13031 8492 ]]intint_Columnint_RowComplex_Column[ (17222,142) (13064,31286) (19187,7900) (22413,8360) (14270,30974) (235,29170) (19711,30833) (18896,25760) (7285,4667)]original size: [ 9 ]
from file size: [ 9 ][ (17222,142) (13064,31286) (19187,7900) (22413,8360) (14270,30974) (235,29170) (19711,30833) (18896,25760) (7285,4667)]Complex_Columnint_Column[ 12550 140 13694 2695 21624 28019 2125 26576 21694]original size: [ 9 ]
from file size: [ 9 ][ 12550 140 13694
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________12
2695 21624 28019 2125 26576 21694]int_Columnfloat_Column[ 3236.86 3757.43 2481.57 3209.43 668.286 3227.57 3407.29 3640.57 145.429]
original size: [ 9 ]
from file size: [ 9 ][ 3236.86 3757.43 2481.57 3209.43 668.286 3227.57 3407.29 3640.57 145.429][ 3236.86 3757.43 2481.57 3209.43 668.286 3227.57 3407.29 3640.57 145.429]float_Columnstart28.265 seconds
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________13
_Development test Program_file: Test.cpp______________________________________________________________________________________
// Test#include "s_1d.hpp"#include "s_2d.hpp"//#include "complex.hpp"#include "funcprlg.hpp"#include <assert.h>#include <iostream.h>#include <stdlib.h>
#include <time.h>int main(){{cout<<Complex(1,2)<<endl;cout<<(Complex(1,2)==Complex(1,3))<<endl;
Signal_1D<Complex,Row> w(Complex(0,0),5);for (int i=0;i<5;++i){w(i)=Complex(rand(),rand());}
Signal_1D<Complex,Row> w3(Complex(0,0),5);for (i=0;i<5;++i){w3(i)=Complex(rand(),rand());}
cout <<"element"<<endl;
cout<< w(2)<<endl;
w(2)=Complex(1,7);cout<< w(2)<<endl;cout <<"element"<<endl;
cout <<"size"<<endl;cout << w.size()<<endl;
Signal_1D<Complex,Col> w4(Complex(0,0),5);for (i=0;i<5;++i){w4(i)=Complex(rand(),rand());}cout<< w4<<endl;
cout<<"constructor"<<endl;
cout<<"obican i conj"<<endl;cout<<w<<endl;cout<<w.conj()<<endl;cout<<endl;
cout<<"uporedi";cout<<endl;cout<<(w!=w)<<endl;cout<<(w==w.conj())<<endl;cout<<"uporedio"<<endl;//Signal_1D q(6);
//cout<<w-w*w<<endl;
cout<<"w conj";cout<<(w==-w)<<endl;cout<<(w)<<endl;cout<<(!w)<<endl;
w*=w;
Signal_1D<double,Col> w1(1.1,5);for ( i=0;i<5;++i){w1(i)=rand()/sqrt(2);}
cout <<"double element"<<endl;//cout<<(w1.conj())<<endl;//cout<<(w1.conj())<<endl;
int x;cout<< w1<<endl;
Signal_1D<float,Col> w2(1,8);for ( i=0;i<8;++i){w2(i)=(float)rand();}cout<<w2;//cin>>x;cout <<"int element"<<endl;
cout<< w2<<endl;
cout<<"Dot ";cout<<w.dot(w3)<<endl;cout<<"Norm ";cout<<w2.norm()<<endl;
cout<<"Dot ";cout<<w3.dot(w)<<endl;cout<<"Norm ";cout<<w.norm()<<endl;
{Signal_1D<float,Col> w7(1,7);
cout<<w7.size()<<"\n";
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________14
cout<<w2<<"\n";w2=w7;cout<<w3<<"\n";}
cerr<<"sranje \n\n";Signal_1D<int,Col> w7(1,7);
//{Signal_2D<Complex> w8(int(4),8,8);//cout<<w8;}cout<<"gen obris";
Signal_2D<float> w9(float(3),8,8);Signal_2D<float> w10(float(3),8,8);cout<<w9;for (i=0;i<8;++i)for (int j=0;j<8;++j){w9(i,j)=float(i+j/2);};cout<<w9*w10;
//cin>>x;
Signal_2D<float> w11(float(3.12),8,8);
//cin>>x;
cout<<w11;cout<<w11.size();cout<<w2.size();cout<<w7<<" Col";
//template<class T> Signal_1D<T>::orientation=Row;//Signal_1D<>::orientation=Row;cout<<w11<<"original";cout<<w9<<"original";//w9=w9*3;
cout<<1.0f/w9*3.0f<<endl;
cout<<2.0f/(3.0f*w2)<<"chanceg";cout<<w9<<endl;
{Signal_2D<float> w12(float(3.12),3,2);};
cout<<w9.T();for (i=0;i<w2.size()(0);++i) w2(i)=float(i);cout<<w2;cout<<T(w2);
Signal_2D<Complex> w8(Complex(4,3),8,8);cout<<w8.conj();
cout<<w2.conj();cout<<w4.conj();
cout<<w2.min()<<endl;cout<<w4.min()<<endl;
cout<<w2.max()<<endl;cout<<w4.max()<<endl;
cout<<w9.min()<<endl;cout<<w8.min()<<endl;
cout<<w9.max()<<endl;cout<<w8.max()<<endl;cout<<w9.size();cout<<w9(7,7);
//cout<<w2;//cout<<fft(w2,1);
}{Signal_1D<Complex,Col> w2(Complex(3.12,1),8);Signal_1D<Complex,Col> w3(Complex(3.12,1),9);for (int i=0;i<w2.size()(0);++i) w2(i)=Complex(i);cout<<(w2+w2);cout<<T(w2);cout<<fft(w2,1);cout<<(w2=w3);
}
cerr<<"2_D"<<endl;{Signal_2D<Complex> w2(8,8);
for (int i=0;i<w2.size()(0);++i)w2(i,i)=Complex(i,i);
cout<<"2_D";//cout<<(w2(2)=w2(3));cout<<w2;cout<<T(w2.T()(3));cout<<real(fft2(w2,1));//Complex q(3,2);//cout<<q*q*q/q+(q-q)/q*q;}{Signal_2D<Complex> w2(8,8);Signal_2D<Complex> r2(8,8);
for (int i=0;i<w2.size()(0);++i)for(int j=0;j<w2.size()(1);++j)
w2(i,j)=Complex(rand(),rand());
clock_t start, finish;double duration;
start = clock();
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________15
r2=(fft(w2,1));
finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; cout<< duration<<" seconds\n";
}
{ Signal_2D<Complex> w2(8,8);Signal_2D<Complex> w1(8,8);for (int i=0;i<w2.size()(0);++i)
for(int j=0;j<w2.size()(1);++j)w2(i,j)=Complex((double)rand()/7,(double)rand()/7);
cout<<w2; fwrite(w2,"joca1.txt"); cout<<"original size:\t"<<w2.size(); fread(w1,"joca1.txt"); cout<<endl; cout<<"from file size:\t"<<w1.size(); cout<<w1; cout<<typeof(w1)<<endl; cout<<typeof(w1(1))<<endl; cout<<typeof(T(w1(1)))<<endl;
cout<<w2(2,2)<<" "<<real(w2(2,2))<<endl;}{ Signal_2D<double> w2(8,8);Signal_2D<double> w1(8,8);for (int i=0;i<w2.size()(0);++i)
for(int j=0;j<w2.size()(1);++j)w2(i,j)=(double)rand()/7;
cout<<w2; fwrite(w2,"joca2.txt"); cout<<"original size:\t"<<w2.size(); fread(w1,"joca2.txt"); cout<<endl; cout<<"from file size:\t"<<w1.size(); cout<<w1; cout<<typeof(w1)<<endl; cout<<typeof(w1(1))<<endl; cout<<typeof(T(w1(1)))<<endl;
}{Signal_2D<int> w2(8,8);Signal_2D<int> w1(8,8);for (int i=0;i<w2.size()(0);++i)
for(int j=0;j<w2.size()(1);++j)w2(i,j)=rand();
cout<<w2; fwrite(w2,"joca3.txt"); cout<<"original size:\t"<<w2.size(); fread(w1,"joca3.txt");
Signal_2D<int> w3("joca3.txt"); cerr<<"after"; cout<<endl; cout<<w1<<endl; cout<<"from file size:\t"<<w3.size(); cout<<w3; cout<<typeof(w1)<<endl; cout<<typeof(w1(1))<<endl; cout<<typeof(T(w1(1)))<<endl;
}
{ Signal_1D<Complex,Col> w2(9);Signal_1D<Complex,Col> w1(9);for (int i=0;i<w2.size()(0);++i)
w2(i)=Complex(rand(),rand());cout<<typeof(w2);cout<<w2;
fwrite(w2,"joca1.txt"); fwrite(T(w2),"joca2.txt");
cout<<"original size:\t"<<w2.size(); fread(w1,"joca1.txt"); cout<<endl; cout<<"from file size:\t"<<w1.size(); cout<<w1; cout<<typeof(w1)<<endl; //cout<<typeof(w1(1))<<endl; //cout<<typeof(w1.T())<<endl;
//cout<<w2(2)<<" "<<real(w2(2))<<endl;}{ Signal_1D<int,Col> w2(9);Signal_1D<int,Col> w1(9);for (int i=0;i<w2.size()(0);++i)
w2(i)=rand();cout<<typeof(w2);cout<<w2;
fwrite(w2,"joca1.txt"); fwrite(T(w2),"joca2.txt");
cout<<"original size:\t"<<w2.size(); fread(w1,"joca1.txt"); cout<<endl; cout<<"from file size:\t"<<w1.size(); cout<<w1; cout<<typeof(w1)<<endl; //cout<<typeof(w1(1))<<endl; //cout<<typeof(w1.T())<<endl;
//cout<<w2(2)<<" "<<real(w2(2))<<endl;}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________16
{ Signal_1D<float,Col> w2(9);Signal_1D<float,Col> w1(9);for (int i=0;i<w2.size()(0);++i)
w2(i)=(float)(rand())/7;cout<<typeof(w2);cout<<w2;
fwrite(w2,"joca1.txt"); fwrite(T(w2),"joca2.txt");
cout<<endl;
Signal_1D<float,Col> w3("joca1.txt");
cout<<"original size:\t"<<w2.size(); fread(w1,"joca1.txt"); cout<<endl; cout<<"from file size:\t"<<w1.size(); cout<<w1; cout<<w3; cout<<typeof(w1)<<endl; //cout<<typeof(w1(1))<<endl; //cout<<typeof(w1.T())<<endl;
//cout<<w2(2)<<" "<<real(w2(2))<<endl;}
clock_t start, finish;double duration;
Signal_2D<Complex> w2(1024,1024);for (int i=0;i<w2.size()(0);++i) w2(i,i)=Complex(i,1);//cout<<w2;//cout<<T(w2);//int x;//cin>>x;//while (0==0){start = clock();cout<<"start"<<endl;fft(w2,1);
finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; cout<< duration<<" seconds\n";}int x;cin>>x;return 0;}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________17
________________________________________________________________________________________
file: Complex.Hpp_____________________________________________________________________________________________
// Complex class for ECE 449 HW#1 Sping ’99// autor: Jovan Brankov#ifndef COMPLEX /* Inhibit repeated inclusion */
#include <math.h> /* For fmod, modf, floor, log10 */#include <iostream.h>#ifndef DOUBLE #define DOUBLE const double#endif
#define COMPLEX const Complex
class Complex {public:
static char rep; // for representation x+i*y, (x,y) , [x,<y] rep= 1,2 or 3 repectivly default 1
// Internal representation: we use real and imaginar parts// -----------------------protected:
double rp,ip;
// Constructors:// ------------public:
Complex(double x): rp(x), ip(0){}Complex(double x , double y) : rp(x), ip(y) {} //(for integer,float automatic conversion)Complex () {}
// The compiler will supply appropriate versions of:// - the destructor,// - the copy constructor,// - the assignment operator.
// Accessor functions :// --------------------public:
Complex conj() const {return Complex(rp,-ip);}double realPart() const {return rp;}double imagPart() const {return ip;}double abs() const {return sqrt(rp*rp+ip*ip);}double theta() const {return atan2(ip,rp);}
// Primitive arithmetic member operators for complex and complex:// -------------------------------------------------------------// For efficiency we follow Scott Myers ("More Effective C++", Addison// Wesley) in defining the compound assignment operators as primitive.
Complex& operator+= (COMPLEX rs) {rp += rs.rp; ip += rs.ip; return *this;}Complex& operator-= (COMPLEX rs) {rp -= rs.rp; ip -= rs.ip; return *this;}Complex& operator*= (COMPLEX rs) {Complex tmp ((rp*rs.rp-ip*rs.ip),(rp*rs.ip+ip*rs.rp));
*this=tmp;return *this; }
Complex& operator/= (COMPLEX rs) {Complex tmp ; double del;del=pow(rs.abs(),2);tmp.rp=(rp*rs.rp+ip*rs.ip)/del;tmp.ip=(ip*rs.rp-rp*rs.ip)/del;*this=tmp;return *this; }
Complex operator- () const {Complex result;result.rp = - rp;result.ip = - ip;return result;}
Complex operator+ () const {return *this; }Complex operator! () const {return conj(); }
// Conversion from complex to double:// ----------------------------------
operator double () const {if (ip!=0) cout<<"Conversion Complex to double Error msg : Imaginary partnot zero!\n";return ip==0 ? rp:abs();};
//when complex number is not real// then thn double become magnitude
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________18
// Primitive arithmetic member operators for complex and double (for integer,float automatic conversion):// ---------------------------------------------------------
//Complex& operator= (DOUBLE rs) {rp = rs;ip=0; return *this;}Complex& operator+= (DOUBLE rs) {rp += rs; return *this;}Complex& operator-= (DOUBLE rs) {rp -= rs; return *this;}Complex& operator*= (DOUBLE rs) {rp*=rs;ip*=rs; return *this;}Complex& operator/= (DOUBLE rs) {rp/=rs;ip/=rs; return *this;}
// Relational member operators:// ---------------------------
short operator== (COMPLEX rs) const {return ((rp == rs.rp)&(ip==rs.ip));}short operator!= (COMPLEX rs) const {return (!(*this==rs)); }short operator> (COMPLEX rs) const {return (abs()>rs.abs()); }short operator< (COMPLEX rs) const {return (abs()<rs.abs()); }
short operator>= (COMPLEX rs) const {return (abs()>=rs.abs()); }short operator<= (COMPLEX rs) const {return (abs()<=rs.abs()); }
short operator== (DOUBLE rs) const {return ((rp == rs)&(ip==0)); }short operator!= (DOUBLE rs) const {return (!(*this==rs)); }
// Additional arithmetic member operators:// --------------------------------------// (defined in terms of the primitive ones)
Complex operator+ (COMPLEX rs) const {return Complex(*this) += rs;}Complex operator- (COMPLEX rs) const {return Complex(*this) -= rs;}Complex operator* (COMPLEX rs) const {return Complex(*this) *= rs;}Complex operator/ (COMPLEX rs) const {return Complex(*this) /= rs;}
Complex operator+ (DOUBLE rs) const {return Complex(*this) += rs;}Complex operator- (DOUBLE rs) const {return Complex(*this) -= rs;}Complex operator* (DOUBLE rs) const {return Complex(*this) *= rs;}Complex operator/ (DOUBLE rs) const {return Complex(*this) /= rs;}
};// Additional (non-member) Money operators// ---------------------------------------
ostream& operator<< (ostream& ls, COMPLEX& rs);
inline double abs (DOUBLE& rs) {return rs > 0 ? rs : -rs; }inline double abs (COMPLEX& rs) {return rs.abs(); }inline double theta (COMPLEX& rs) {return rs.theta(); }inline double real (COMPLEX& rs) {return rs.realPart(); }inline double imag (COMPLEX& rs) {return rs.imagPart(); }inline Complex conj (COMPLEX& rs) {return (rs.conj()); }
#endif
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________19
_____________________________________________________________________________________________
file: Complex.Cpp_____________________________________________________________________________________________
// Complex class for ECE 449 HW#1 Sping ’99// autor: Jovan Brankov#include "complex.hpp"
//#define double realPart(Complex rs) {return rs.realPart();}
char Complex::rep=’2’;
ostream& operator<< (ostream& ls, COMPLEX& rs){
switch (Complex::rep){case ’2’:
// (x,y) represention{ ls<<"("<<real(rs)<<","<<imag(rs)<<")";break; }
case ’3’:// (magnitude,angle) represention{ls<<"["<<abs(rs);ls<<",<"<<(theta(rs) * 180 / 3.1415926)<<"]";}; break;
default:// x+i*y external represention{ //may be not a best way to do this
ls<<real(rs);imag(rs)>0 ? ls<<"+": imag(rs)!=0 ? ls<<"-":ls<<"" ;if (rs.imagPart()!=0 ) //not plot 0 for imaginary part{
if ((abs(rs.imagPart()))!=1)ls<< abs(rs.imagPart());
ls<<"i";};break;
}}
return ls;}
//char* toString(COMPLEX rs)
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________20
_____________________________________________________________________________________________
file: Signal_1D.Hpp_____________________________________________________________________________________________
// Signal_1D class for ECE 449 project#1 Sping ’99// autor: Jovan Brankov#ifndef SIGNAL_1D /* Inhibit repeated inclusion */
#include <math.h> /* For fmod, modf, floor, log10 */#include <iostream.h>#include <assert.h>#include <string.h>#include <stdio.h>
//#include "funcprlg.hpp"#include "utilmac.hpp"#include "complex.hpp"
#define SIGNAL_1D const Signal_1D#define Complexs Complex*#define Row ’R’#define Col ’C’
//declaration in advancetemplate<class Tc,char s> class Signal_1D;template<class Tc> class Signal_2D;
template <class Tc> int fread(Signal_1D<Tc,Col>& rs, char name[]);template <class Tc> int fread(Signal_1D<Tc,Row>& rs, char name[]);
//SIGNAL_1D
template<class Tc,char s>class Signal_1D {
// Internal representation// -----------------------protected:
Tc* S_1D;int num;
// Constructors:// ------------public:
// from another Signal_1D Signal_1D (SIGNAL_1D& rs){num=rs.num;
S_1D=new Tc[num];for (int i=0;i<num;++i)
{S_1D[i]=rs.S_1D[i]; // same as *(S_1D+i)};
}
// set value d in s size Signal_1DSignal_1D (Tc d,int s) { num =s; S_1D=new Tc[s];
for (int i=0;i<num;++i){ *(S_1D+i)=d;}
}// set value 0 in s size Signal_1D
Signal_1D (int s) { num =s; S_1D=new Tc[s];}
// from fileSignal_1D (char name[]){int M;
FILE *stream; char rtype[10];if( (stream = fopen( name, "r" )) != NULL )
{fscanf( stream, "%i \n %s \n",&M,&rtype);num =M; S_1D=new Tc[num];fread(*this,name);
}else {
cerr<< "Unable to open file";assert(0);}
}//default constructor
Signal_1D () {S_1D=NULL;num=0;};
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________21
// destuctor// --------------------
~Signal_1D () { delete[] S_1D;}
// Accessor functions :// --------------------public: Signal_1D conj() const;
Signal_1D<int,Row> size() const {return Signal_1D<int,Row> (num,1);};Tc dot(SIGNAL_1D& rs) const;double norm() const;//Signal_1D conv() const;Tc max() const;Tc min() const;Tc& operator () (int n) const{ assert(n<num) ;return S_1D[n];} // it can read and store value
// Primitive arithmetic member operators// -------------------------------------------------------------// For efficiency we follow Scott Myers ("More Effective C++", Addison// Wesley) in defining the compound assignment operators as primitive.
Signal_1D& operator= (SIGNAL_1D rs) { if ((num!=0) && (num!=rs.num) ){if (S_1D!=NULL) delete [] S_1D;}
if (num!=rs.num){num=rs.num;S_1D=new Tc[num];}
for (int i=0;i<num;++i){ S_1D[i]=rs.S_1D[i];}
return *this;}
Signal_1D& operator+= (SIGNAL_1D rs) { if (num==rs.num){
for (int i=0;i<num;++i){S_1D[i]+=rs.S_1D[i];}
return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! <+=>";assert(0);return *this;}
Signal_1D& operator-= (SIGNAL_1D rs) { if (num==rs.num){
for (int i=0;i<num;++i){
S_1D[i]-=rs.S_1D[i];}
return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! <-=>";
assert(0);return *this;
}Signal_1D& operator*= (SIGNAL_1D rs) { if (num==rs.num)
{for (int i=0;i<num;++i)
{S_1D[i]*=rs.S_1D[i];
}return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! <*=>";assert(0);return *this;
}Signal_1D& operator/= (SIGNAL_1D rs) { if (num==rs.num)
{for (int i=0;i<num;++i)
{S_1D[i]=S_1D[i]+rs.S_1D[i];
}return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! </=>";assert(0);return *this;
}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________22
Signal_1D& operator- () { for (int i=0;i<num;++i){
S_1D[i]=-S_1D[i];}return *this;
}Signal_1D operator+ () const {return *this; }Signal_1D operator! () const {return conj(); }
// Relational member operators:// ---------------------------
short operator== (SIGNAL_1D rs) const { short rez(1); int i(num); for (i=0;i<num;i++)
{rez*=(S_1D[i]==rs.S_1D[i]);}
return rez;}
short operator!= (SIGNAL_1D rs) const {return (!(*this==rs)); }
// Additional arithmetic member operators:// --------------------------------------// (defined in terms of the primitive ones)
Signal_1D operator+ (SIGNAL_1D rs) const {return Signal_1D(*this) += rs;}Signal_1D operator- (SIGNAL_1D rs) const {return Signal_1D(*this) -= rs;}Signal_1D operator* (SIGNAL_1D rs) const {return Signal_1D(*this) *= rs;}Signal_1D operator/ (SIGNAL_1D rs) const {return Signal_1D(*this) /= rs;}
Signal_1D operator* (Tc rs) const {for (int i=0;i<num;++i)S_1D[i]=S_1D[i]*rs;
return *this;}Signal_1D operator/ (Tc rs) const {for (int i=0;i<num;++i)
S_1D[i]=S_1D[i]/rs;return *this;}
friend ostream& operator<<(ostream& ls, SIGNAL_1D& rs);
};// end of Signal_1D class definiftion//------------------------------------------------------------------------------------
//definition of acessor function//------------------------------------------------------------------------------------template<class Tc,char s>
Signal_1D<Tc,s> Signal_1D<Tc,s>::conj() const{ Signal_1D<Tc,s> tmp(*this);
return tmp; };Signal_1D<Complex,Row> Signal_1D<Complex,Row>::conj() const
{ Signal_1D<Complex,Row> tmp(num);for (int i=0;i<num;++i){tmp.S_1D[i]=S_1D[i].conj();}return tmp; };
Signal_1D<Complex,Col> Signal_1D<Complex,Col>::conj() const{ Signal_1D<Complex,Col> tmp(num);
for (int i=0;i<num;++i){tmp.S_1D[i]=S_1D[i].conj();}return tmp; };
template<class Tc,char s>Tc Signal_1D<Tc,s>::dot(SIGNAL_1D& rs) const
{ Tc rez(0);if (num==rs.num)
for (int i=0;i<num;++i){rez+=S_1D[i]*rs.S_1D[i];}
else{cerr<< "\nInvalide results-Error msg: Different lenght of vectors! <dot>";
};return rez; };
template<class Tc,char s>double Signal_1D<Tc,s>::norm() const
{ double rez=sqrt(dot(conj()));return rez; };
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________23
template<class Tc,char s>Tc Signal_1D<Tc,s>::max() const
{ Tc tmp(0);for (int i=0;i<num;++i)
if (tmp<=S_1D[i]) tmp=S_1D[i];return tmp;};
template<class Tc,char s>Tc Signal_1D<Tc,s>::min() const
{ Tc tmp(1e6);for (int i=0;i<num;++i)
if (tmp>=S_1D[i]) tmp=S_1D[i];return tmp;};
// Output stream operator//------------------------------------------------------------------------------------
template <class Tc>ostream& operator<<(ostream& ls, SIGNAL_1D<Tc,Row>& rs)
{ ls<<"[ ";for (int i=0;i<rs.num;++i){//ls<<((i!=0) ? "\t":"");
ls << rs.S_1D[i]<<" ";}return ls<<"]"<<endl;};
template <class Tc>ostream& operator<<(ostream& ls, SIGNAL_1D<Tc,Col>& rs)
{ ls<<"[ \n";for (int i=0;i<rs.num;++i)
ls <<" "<< rs.S_1D[i]<<endl;return ls<<"]"<<endl;};
// Additional (non-member) Money operators//------------------------------------------------------------------------------------//
template <class Tc> Signal_1D<Tc,Col> operator* (Tc ls,Signal_1D<Tc,Col> rs) {return (rs*ls);};template <class Tc> Signal_1D<Tc,Row> operator* (Tc ls,Signal_1D<Tc,Row> rs) {return (rs*ls);};template <class Tc> Signal_2D<Tc> operator* (Signal_1D<Tc,Col> ls, Signal_1D<Tc,Row> rs)
{int M=rs.size()(0);int N=rs.size()(0);Signal_2D<Tc>tmp(M,N);for (int i=0;i<M;++i)
for (int j=0;j<N;++j)tmp(i,j)=ls(i)*rs(j);
return tmp;};
template <class Tc> Tc operator* (Signal_1D<Tc,Row> ls, Signal_1D<Tc,Col> rs) {return(rs.dot(T(ls)));};template <class Tc> Signal_1D<Tc,Row> operator/ (Tc ls,SIGNAL_1D<Tc,Row> rs)
{Signal_1D<Tc,Row> tmp(rs);for (int i=0;i<(rs.size()(0));++i)
tmp(i)=ls/rs(i);return tmp;}
template <class Tc> Signal_1D<Tc,Col> operator/ (Tc ls,SIGNAL_1D<Tc,Col> rs){Signal_1D<Tc,Col> tmp(rs);for (int i=0;i<(rs.size()(0));++i)
tmp(i)=ls/rs(i);return tmp;}
template<class Tc>Signal_1D<Tc,Row> T(SIGNAL_1D<Tc,Col>& rs) { int num=rs.size()(0);
Signal_1D<Tc,Row> tmp(num);for (int i=0;i<num;++i)
tmp(i)=rs(i);return tmp;};
template<class Tc>Signal_1D<Tc,Col> T(SIGNAL_1D<Tc,Row>& rs) { int num=rs.size()(0);
Signal_1D<Tc,Col> tmp(num);for (int i=0;i<num;++i)
tmp(i)=rs(i);return tmp;};
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________24
template<class Tc>Signal_1D<Complex,Row> fft(SIGNAL_1D<Tc,Row>& rs, INT isign){return T(fft(T(rs),isign));};
template<class Tc>Signal_1D<Complex,Col> fft(SIGNAL_1D<Tc,Col>& rs, INT isign){
int gamma, i, nbits=0, m, n2, nu1, k, I, M, P;float *wpre, *wpim, phi, dphi;
int n=rs.size()(0),d=-isign;Signal_1D<Complex,Col> sig(n);for (i=0;i<n;++i) sig(i)=Complex(rs(i));Complex tmp;//if (d!=1 && d!=-1) return 0;/* assume n is exponent of 2, so that one and only one *//* bit of n is nonzero, and the position of the bit *//* equals to the exponent */
for (i=0, k=n; k>0; k>>=1, ++i){
if (k&0x0001) gamma=i, ++nbits;}if (nbits != 1) return 0;
/* initialize */
m=1, n2=n>>1, nu1=gamma-m, k=0;
if (!(wpre = (float *) calloc(sizeof(float), n2))){
return 0;}if (!(wpim = (float *) calloc(sizeof(float), n2))){
free(wpre);return 0;
}for (i=0, phi=0, dphi=(float)(2.*3.1415926535/n); i<n2; ++i, phi+=dphi){
if (d==-1) wpre[i]=(float)cos(-phi), wpim[i]=(float)sin(-phi);else wpre[i]=(float)cos(phi), wpim[i]=(float)sin(phi);
}/* fft loop */while (m<=gamma){
while (k<n){
for (I=0; I<n2; ++I, ++k){
/* right shift k nu1 bits and bit reverse */for (i=0, M=k; i<nu1; ++i) M>>=1;for (i=P=0; i<gamma; ++i, M>>=1){
P<<=1;if (M&0x0001) P+=1;
}/* dual node update */tmp=sig(k+n2)*Complex(wpre[P],wpim[P]);sig(k+n2) =sig(k)-tmp;sig(k)+=tmp;
}k+=n2;
}++m, n2>>=1, --nu1, k=0;
}/* unscrambling */for (k=0; k<n; ++k){
for (i=P=0, M=k; i<gamma; ++i, M>>=1){
P<<=1;if (M&0x0001) P+=1;
}if (P<=k){
tmp=sig(k);sig(k)=sig(P);sig(P)=tmp;
}}/* return ok */
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________25
if (d==1){
for (i=0; i<n; ++i) sig(i)/=n;}free(wpre); free(wpim); return sig;
}
Signal_1D<double,Row> real(SIGNAL_1D<Complex,Row>& rs){int num=rs.size()(0);Signal_1D<double,Row> tmp(num);for(int i=0;i<num;++i)
tmp(i)=real(rs(i));return tmp; }
Signal_1D<double,Col> real(SIGNAL_1D<Complex,Col>& rs){int num=rs.size()(0);Signal_1D<double,Col> tmp(num);for(int i=0;i<num;++i)
tmp(i)=real(rs(i));return tmp; };
Signal_1D<double,Row> imag(SIGNAL_1D<Complex,Row>& rs){int num=rs.size()(0);Signal_1D<double,Row> tmp(num);for(int i=0;i<num;++i)
tmp(i)=imag(rs(i));return tmp; }
Signal_1D<double,Col> imag(SIGNAL_1D<Complex,Col>& rs){int num=rs.size()(0);Signal_1D<double,Col> tmp(num);for(int i=0;i<num;++i)
tmp(i)=imag(rs(i));return tmp; };
char* typeof(SIGNAL_1D<Complex,Col>& rs ) {return "Complex_Column"; };char* typeof(SIGNAL_1D<double,Col>& rs ) {return "double_Column";};char* typeof(SIGNAL_1D<float,Col>& rs ) {return "float_Column"; };char* typeof(SIGNAL_1D<int,Col>& rs ) {return "int_Column"; };
char* typeof(SIGNAL_1D<Complex,Row>& rs ) {return "Complex_Row"; };char* typeof(SIGNAL_1D<double,Row>& rs ) {return "double_Row"; };char* typeof(SIGNAL_1D<float,Row>& rs ) {return "float_Row"; };char* typeof(SIGNAL_1D<int,Row>& rs ) {return "int_Row"; };
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________26
template<class Tc> _fwrite(SIGNAL_1D<Tc,Col>rs, char name[],char type[10]){int M=rs.size()(0);FILE *stream;int i;float buf1,buf2;if( (stream = fopen( name, "w+" )) != NULL ){
fprintf( stream, "%i \n",M);fprintf( stream,"%s\n", type);
if ((strcmp(type,"Complex_Row")==0) | (strcmp(type,"Complex_Column")==0)){Complex tmp(0,0);
for ( i = 0; i < M; i++ ){ tmp=rs(i);
buf1=(float)real(tmp);buf2=(float)imag(tmp);fprintf( stream, "%f ", buf1);fprintf( stream, "%f \t", buf2);if (strcmp(type,"Complex Column")==0)
fprintf( stream,"\n");}fprintf( stream, "\n" );
}else if ((strcmp(type,"int_Row")!=0)&(strcmp(type,"int_Column")!=0))
{for ( i = 0; i < M; i++ ){fprintf( stream, "%f \t", (float)rs(i) );if ((strcmp(type,"double_Column")==0)|(strcmp(type,"float_Column")==0))
fprintf( stream,"\n");
}fprintf( stream, "\n" );}
else {for ( i = 0; i < M; i++ ){
fprintf( stream, "%i \t", rs(i) );if (strcmp(type,"int_Column")==0)
fprintf( stream,"\n");}
fprintf( stream, "\n" );}
fclose( stream );}else
return 0;return 1;
}template <class Tc>
int fwrite(SIGNAL_1D<Tc,Col>& rs, char name[]){return _fwrite(rs,name,typeof(rs));}template <class Tc>
int fwrite(SIGNAL_1D<Tc,Row>& rs, char name[]){return _fwrite(T(rs),name,typeof(rs));}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________27
template <class Tc>int _fread(Signal_1D<Tc,Col>& rs,char name[],char type[]){
int M;FILE *stream; int i;char rtype[10];
float buf1,buf2;
if( (stream = fopen( name, "r" )) != NULL ){
fscanf( stream, "%i \n %s \n",&M,&rtype);if (strcmp(rtype,type)!=0) {cerr<<"Different type in a file\n";assert(0);};
if ((strcmp(type,"Complex_Row")==0) | (strcmp(type,"Complex_Column")==0)){
for ( i = 0; i < M; ++i ){
fscanf( stream, "%f", &buf1);fscanf( stream, "%f \t", &buf2);rs(i)=(Tc)Complex(buf1,buf2);
}
}else if ((strcmp(type,"int_Row")!=0)&(strcmp(type,"int_Column")!=0))
{for ( i = 0; i < M; ++i ){ Tc tmp;fscanf( stream, "%f ", &tmp);rs(i)=(Tc)tmp;}
}else {
for ( i = 0; i < M; ++i ){ Tc tmp;
fscanf( stream, "%i ", &tmp );rs(i)=(Tc)tmp;}
}fclose( stream );
}else
return 0;return 1;
}
template <class Tc>int fread(Signal_1D<Tc,Col>& rs, char name[]){return _fread(rs,name,typeof(rs));}
template <class Tc>int fread(Signal_1D<Tc,Row>& rs, char name[]){Signal_1D<Tc,Col> tmp(T(rs));
int i=_fread(tmp,name,typeof(rs));rs=T(tmp);return i;}
#endif
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________28
_____________________________________________________________________________________________
file: Signal_2D.Hpp_____________________________________________________________________________________________
// Signal_2D class for ECE 449 project#1 Sping ’99// autor: Jovan Brankov#ifndef SIGNAL_2D /* Inhibit repeated inclusion */
#include <math.h> /* For fmod, modf, floor, log10 */#include <iostream.h>#include <assert.h>#include <stdio.h>#include <string.h>
#include "s_1d.hpp"
#define SIGNAL_2D const Signal_2D
template <class Tc> class Signal_2D;template <class Tc> int fread(Signal_2D<Tc>& rs, char name[]);
template<class Tc>
class Signal_2D {
// Internal representation// -----------------------
protected:Signal_1D<Tc,Col>* S_2D;int num;
// Constructors:// ------------public:// from another Signal_2D
Signal_2D (SIGNAL_2D& rs) {int M=rs.size()(0);int N=rs.size()(1);S_2D=new Signal_1D<Tc,Col>[M];num=N;for (int i=0;i<num;++i)
{S_2D[i]=rs.S_2D[i]; // same as *(S_2D+i)};
}// set value d in s size Signal_2D
Signal_2D (Tc d,int M,int N) {Signal_1D<Tc,Col>tmp(d,M);S_2D=new Signal_1D<Tc,Col>[M];num=N;for (int i=0;i<num;++i)
{S_2D[i]=tmp;}}
// set value 0 in s size Signal_2DSignal_2D (int M,int N) { Signal_1D<Tc,Col> tmp(M);
S_2D=new Signal_1D<Tc,Col>[M];num =N;for (int i=0;i<num;++i)
{*(S_2D+i)=tmp;}
}// from file
Signal_2D (char name[]){int M,N;
FILE *stream;char rtype[10];if ((stream = fopen( name, "r" )) != NULL )
{fscanf( stream, "%i %i \n %s",&M,&N,rtype);num =N; Signal_1D<Tc,Col> tmp(M);S_2D=new Signal_1D<Tc,Col>[num];for (int j=0;j<num;++j) S_2D[j]=tmp;fread(*this,name);}
else {cerr<< "Unable to open file";assert(0);}
}/
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________29
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________30
/ default constructorSignal_2D (){num=0;S_2D=new Signal_1D<Tc,Col>[0];};
// destuctor~Signal_2D () {
assert( S_2D != NULL ); /* Cannot be NULL */assert( *S_2D != ’\0’ );/* Cannot be empty */
//for (int i=0;i<num;++i) //this is not nessesery!!// { ‘// delete [] *(S_2D+i);// object;// }delete[] S_2D;}
// Accessor functions :// --------------------public: Signal_2D conj() const;
Signal_1D<int,Row> size() const {Signal_1D<int,Row> qw(2);qw(0)=S_2D[0].size()(0);qw(1)=num;return qw;};
Tc dot(Signal_2D rs) const;Signal_2D T() const;//double det() const;//Signal_2D conv() const;Tc max() const;Tc min() const;
Tc& operator () (int m,int n) const{ assert(((n<(num))&&(m<(S_2D[0].size()(0)))));return S_2D[n](m);}
Signal_1D<Tc,Col>& operator () (int n) const{ //assert(!((m<num)&&(n<S_2D[0].size()(0))));return S_2D[n];}
// Primitive arithmetic member operators// -------------------------------------------------------------// For efficiency we follow Scott Myers ("More Effective C++", Addison// Wesley) in defining the compound assignment operators as primitive.
Signal_2D& operator= (SIGNAL_2D& rs){if ((num!=rs.num) || (S_2D[0].size()(0)!=rs.S_2D[0].size()(0))){if (S_2D!=NULL) delete [] S_2D;num=rs.num;S_2D=new Signal_1D<Tc,Col>[num];}for (int i=0;i<num;++i)
{S_2D[i]=rs.S_2D[i]; // same as *(S_2D+i)};
return *this;}
Signal_2D& operator+= (SIGNAL_2D& rs) { if (num==rs.num){for (int i=0;i<num;++i)
S_2D[i]+=rs.S_2D[i];return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! <+=>";assert(0);return *this;}
Signal_2D& operator-= (SIGNAL_2D& rs) { if (num==rs.num){for (int i=0;i<num;++i)
S_2D[i]-=rs.S_2D[i];return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! <-=>";assert(0);return *this;}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________31
Signal_2D& operator*= (SIGNAL_2D& rs) { if (num==rs.num){for (int i=0;i<num;++i)
S_2D[i]*=rs.S_2D[i];return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! <*=>";assert(0);return *this;}
Signal_2D& operator/= (SIGNAL_2D& rs) { if (num==rs.num){for (int i=0;i<num;++i)
S_2D[i]=S_2D[i]+rs.S_2D[i];return *this;}
cerr<< "Invalide results-Error msg: Different lenght of vectors! </=>";assert(0);return *this;}
Signal_2D& operator- () { for (int i=0;i<num;++i) S_2D[i]=-S_2D[i]; return *this;}
Signal_2D operator+ () const {return *this; }Signal_2D operator! () const {return conj();}
// Relational member operators:// ---------------------------
short operator== (SIGNAL_2D& rs) const { short rez(1); int i(num); for (i=0;i<num;i++)
{rez*=(S_2D[i]==rs.S_2D[i]);}
return rez;}
short operator!= (SIGNAL_2D& rs) const {return (!(*this==rs)); }
// Additional arithmetic member operators:// --------------------------------------// (defined in terms of the primitive ones)
Signal_2D operator+ (SIGNAL_2D& rs) const {return Signal_2D(*this) += rs;}Signal_2D operator- (SIGNAL_2D& rs) const {return Signal_2D(*this) -= rs;}Signal_2D operator* (SIGNAL_2D& rs) const {return Signal_2D(*this) *= rs;}Signal_2D operator/ (SIGNAL_2D& rs) const {return Signal_2D(*this) /= rs;}Signal_2D operator* (const Tc rs) const {for (int i=0;i<num;++i)
S_2D[i]=S_2D[i]*rs;return *this;}
Signal_2D operator/ (const Tc rs) const {for (int i=0;i<num;++i)S_2D[i]=S_2D[i]/rs;
return *this;}
friend ostream& operator<<(ostream& ls, SIGNAL_2D& rs);
};// end of Signal_2D class definiftion//------------------------------------------------------------------------------------
//definition of acessor function//------------------------------------------------------------------------------------
template<class Tc> Signal_2D<Tc>Signal_2D<Tc>::T() const
{ int M=size()(0);int N=size()(1);Signal_2D<Tc> tmp(*this);for (int i=0;i<M;++i)
for (int j=0;j<N;++j)tmp(j,i)=S_2D[j](i);
return tmp;};
template<class Tc> Signal_2D<Tc>Signal_2D<Tc>::conj() const
{ Signal_2D<Tc> tmp(*this);for (int i=0;i<num;++i){tmp.S_2D[i]=S_2D[i].conj();}return tmp;
};
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________32
template<class Tc,char s>Tc Signal_2D<Tc>::max() const
{ Tc tmp(0);for (int i=0;i<num;++i)
if (tmp<=S_2D[i].max())tmp=S_2D[i].max();
return tmp;};template<class Tc,char s>
Tc Signal_2D<Tc>:: min() const{ Tc tmp(1e34);
for (int i=0;i<num;++i)if (tmp>=S_2D[i].min())
tmp=S_2D[i].min();return tmp;};
// Output stream operator// ----------------------template <class Tc>
ostream& operator<<(ostream& ls, SIGNAL_2D<Tc>& rs){ int M=rs.size()(0);int N=rs.size()(1);ls<<"[ \n";Tc dummy;dummy=1/10000;for (int i=0;i<M;++i)
{ls<<"[ ";for (int j=0;j<N;++j)
ls << rs(i,j)*Tc(abs(rs(i,j))>dummy)<<"\t";ls<<"]"<<endl;}
return ls<<"]"<<endl;};
// Additional (non-member) operators//------------------------------------------------------------------------------------
template<class Tc> Signal_2D<Tc> operator* (const Tc ls ,SIGNAL_2D<Tc>& rs) {return (rs*ls);}template<class Tc> Signal_2D<Tc> operator/ (const Tc ls ,SIGNAL_2D<Tc>& rs)
{ Signal_2D<Tc> tmp(rs);for (int i=0;i<rs.size()(0);++i)
tmp(i)=ls/rs(i);return tmp;}f
template <class Tc>Signal_2D<Complex> fft(SIGNAL_2D<Tc>& rs, const int isign)
{int M=rs.size()(0); int N=rs.size()(1);Signal_2D<Complex> tmp(Complex(0,0),M,N);for (int i=0;i<N;++i){
tmp(i)=fft(rs(i),isign);}return tmp; }
template <class Tc>Signal_2D<Complex> fft2(SIGNAL_2D<Tc>& rs, const int isign)
{int M=rs.size()(0); int N=rs.size()(1);Signal_2D<Complex> tmp(M,N);for (int i=0;i<N;++i)
tmp(i)=fft(rs(i),isign);Signal_2D<Complex> tmp2(tmp.T());for (i=0;i<M;++i)
tmp(i)=fft(tmp2(i),isign);return tmp.T(); }
Signal_2D<double> real(SIGNAL_2D<Complex>& rs){int M=rs.size()(0); int N=rs.size()(1);Signal_2D<double> tmp(M,N);for(int i=0;i<N;++i)
tmp(i)=real(rs(i));return tmp; }
Signal_2D<double> imag(SIGNAL_2D<Complex>& rs){int M=rs.size()(0); int N=rs.size()(1);Signal_2D<double> tmp(M,N);for(int i=0;i<N;++i)
tmp(i)=imag(rs(i));return tmp; }
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________33
template <class Tc>int fwrite(SIGNAL_2D<Tc>& rs,char name[]){char type[10];strcpy(type,typeof(rs));
int M=rs.size()(0); int N=rs.size()(1);FILE *stream; int i,j;float buf1=0,buf2;
if( (stream = fopen( name, "w+" )) != NULL ){
fprintf( stream, "%i %i\n",M,N);fprintf( stream,"%s\n", type);
if (strcmp(type,"Complex")==0){Complex tmp(0,0);for ( i = 0; i < M; i++ ){ for ( j = 0; j < N; j++ )
{ tmp=rs(i,j);buf1=(float)real(tmp);buf2=(float)imag(rs(i,j));fprintf( stream, "%f ", buf1);
fprintf( stream, "%f \t", buf2);}fprintf( stream, "\n" );
}}
else if(strcmp(type,"int")!=0)for ( i = 0; i < M; i++ ){ for ( j = 0; j < N; j++ )
fprintf( stream, "%f \t", (float)rs(i,j) );fprintf( stream, "\n" );
}else
for ( i = 0; i < M; i++ ){ for ( j = 0; j < N; j++ )
fprintf( stream, "%i \t", rs(i,j) );fprintf( stream, "\n" );
}fclose( stream );
}else
return 0;return 1;
}
______________________________________________________________________________________________________________
___________________________________________________________________________________________________________34
template <class Tc>int fread(Signal_2D<Tc>& rs,char name[]){char type[10];strcpy(type,typeof(rs));int M,N;FILE *stream;int i,j;char rtype[10];
if( (stream = fopen( name, "r" )) != NULL ){
fscanf( stream, "%i %i \n %s",&M,&N,rtype);if (strcmp(rtype,type)!=0) {cerr<<"Different type in a file\n";assert(0);};
if (strcmp(type,"Complex")==0){float buf1,buf2;Complex(tmp);for ( i = 0; i < M; i++ )
{for ( j = 0; j < N; j++ ){fscanf( stream, "%f ", &buf1);
fscanf( stream, "%f ", &buf2);rs(i,j)=(Tc)Complex(buf1,buf2);}
}}
else if(strcmp(type,"int")!=0){float buf;for ( i = 0; i < M; i++ )
{for ( j = 0; j < N; j++ ){fscanf( stream, "%f ", &buf);rs(i,j)=(Tc)buf;}
}}
else{int buf;for ( i = 0; i < M; i++ )
{for ( j = 0; j < N; j++ ){fscanf( stream, "%i ", &buf );rs(i,j)=(Tc)buf;}
}}
fclose( stream );}else
return 0;return 1;}
char* typeof(SIGNAL_2D<Complex>& rs ) {return "Complex"; };char* typeof(SIGNAL_2D<double>& rs ) {return "double"; };char* typeof(SIGNAL_2D<float>& rs ) {return "float"; };char* typeof(SIGNAL_2D<int>& rs ) {return "int"; };
#endif