Thursday, January 27, 2011

DFT matlab & gcc


matlab

fs=[ ];
Ys=abs(fft(Yt));
plot(fs,Ys);




fs = 1/t
------------------------------------------------------------------------------------
gcc

#include <math.h>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
 int k = 0,n = 0,xn=0, N = 250;
 double x[N],y[N],r[N],i[N],real=0,imagin=0,PI=3.1415926;
 string filename,outfilename;
   ifstream instream;
  
   cout<<"file"<<endl;
 cin>>filename;                        
 instream.open(filename.c_str());
 cout <<"successful"<<endl;

while(xn != N)
{
 instream >> x[xn];
 cout << "x"<<xn << " = " << x[xn] << endl;
 xn++;
 }
for(n=0;n!=N;n++)
{
 for(k=0;k!=N;k++)
 {
  r[k] = x[k]*sin(2*PI*k*n/N);
  i[k] = x[k]*cos(2*PI*k*n/N);
  real = real + r[k];
  cout<<"r "<<k<<"= "<<r[k]<<endl;
  imagin=imagin+i[k];
  cout<<"i "<<k<<"= "<<i[k]<<endl;
 }
 cout<<"real "<<n<<"= "<<real<<endl;
 cout<<"imagin"<<n<<"= "<<imagin<<endl;
 y[n]=sqrt(real*real+imagin*imagin);
 cout<<"y "<<n<<"= "<<y[n]<<"----------------------------"<<endl;
 }

cout<<"outfile"<<endl;
cin>>outfilename;
ofstream outfile(outfilename.c_str());
for(xn=0;xn!=N;xn++)
{
 outfile<<100/(xn+1)<<" "<<y[xn]<<endl;
 }

cout<<"done"<<endl;
return 0;
 }


-------------------------------------------------------------------------------------------------
fs=[
100.0000
50.0000
33.3333
25.0000
20.0000
16.6667
14.2857
12.5000
11.1111
10.0000
9.0909
8.3333
7.6923
7.1429
6.6667
6.2500
5.8824
5.5556
5.2632
5.0000
4.7619
4.5455
4.3478
4.1667
4.0000
3.8462
3.7037
3.5714
3.4483
3.3333
3.2258
3.1250
3.0303
2.9412
2.8571
2.7778
2.7027
2.6316
2.5641
2.5000
2.4390
2.3810
2.3256
2.2727
2.2222
2.1739
2.1277
2.0833
2.0408
2.0000
1.9608
1.9231
1.8868
1.8519
1.8182
1.7857
1.7544
1.7241
1.6949
1.6667
1.6393
1.6129
1.5873
1.5625
1.5385
1.5152
1.4925
1.4706
1.4493
1.4286
1.4085
1.3889
1.3699
1.3514
1.3333
1.3158
1.2987
1.2821
1.2658
1.2500
1.2346
1.2195
1.2048
1.1905
1.1765
1.1628
1.1494
1.1364
1.1236
1.1111
1.0989
1.0870
1.0753
1.0638
1.0526
1.0417
1.0309
1.0204
1.0101
1.0000
0.9901
0.9804
0.9709
0.9615
0.9524
0.9434
0.9346
0.9259
0.9174
0.9091
0.9009
0.8929
0.8850
0.8772
0.8696
0.8621
0.8547
0.8475
0.8403
0.8333
0.8264
0.8197
0.8130
0.8065
0.8000
0.7937
0.7874
0.7813
0.7752
0.7692
0.7634
0.7576
0.7519
0.7463
0.7407
0.7353
0.7299
0.7246
0.7194
0.7143
0.7092
0.7042
0.6993
0.6944
0.6897
0.6849
0.6803
0.6757
0.6711
0.6667
0.6623
0.6579
0.6536
0.6494
0.6452
0.6410
0.6369
0.6329
0.6289
0.6250
0.6211
0.6173
0.6135
0.6098
0.6061
0.6024
0.5988
0.5952
0.5917
0.5882
0.5848
0.5814
0.5780
0.5747
0.5714
0.5682
0.5650
0.5618
0.5587
0.5556
0.5525
0.5495
0.5464
0.5435
0.5405
0.5376
0.5348
0.5319
0.5291
0.5263
0.5236
0.5208
0.5181
0.5155
0.5128
0.5102
0.5076
0.5051
0.5025
0.5000
0.4975
0.4950
0.4926
0.4902
0.4878
0.4854
0.4831
0.4808
0.4785
0.4762
0.4739
0.4717
0.4695
0.4673
0.4651
0.4630
0.4608
0.4587
0.4566
0.4545
0.4525
0.4505
0.4484
0.4464
0.4444
0.4425
0.4405
0.4386
0.4367
0.4348
0.4329
0.4310
0.4292
0.4274
0.4255
0.4237
0.4219
0.4202
0.4184
0.4167
0.4149
0.4132
0.4115
0.4098
0.4082
0.4065
0.4049
0.4032
0.4016
0.4000
]

Yt=[0.03
-0.02
0.035
-0.015
0.04
-0.01
0.045
-0.005
0.05
0.0
0.055
0.005
0.06
0.01
0.065
0.015
0.07
0.02
0.08
0.03
0.09
0.04
0.1
0.07
0.09
0.04
0.08
0.03
0.07
0.02
0.06
0.01
0.055
0.005
0.05
0.0
0.04
-0.01
0.03
-0.02
0.02
-0.03
0.02
-0.03
0.02
-0.04
0
-0.05
-0.02
-0.07
-0.05
-0.1
0.05
0.25
0.45
0.55
0.67
1
0.65
0.4
0.3
0.15
0
-0.05
-0.15
-0.21
-0.27
-0.25
-0.22
-0.1
0
0
-0.03
0.02
-0.01
0.04
-0.01
0.04
-0.01
0.04
-0.05
0.045
0.0
0.05
0.05
0.0
0.055
0.005
0.06
0.01
0.08
0.03
0.15
0.1
0.175
0.125
0.18
0.13
0.185
0.135
0.2
0.19
0.14
0.16
0.11
0.14
0.09
0.12
0.07
0.1
0.05
0.08
0.03
0.05
0
0.025
-0.025
0.025
-0.025
0.025
-0.025
0.03
-0.02
0.03
-0.02
0.035
-0.015
0.035
-0.015
0.035
-0.015
0.035
-0.015
0.03
-0.02
0.03
-0.02
0.03
-0.02
0.025
0.03
-0.02
0.035
-0.015
0.04
-0.01
0.045
-0.005
0.05
0.0
0.055
0.005
0.06
0.01
0.065
0.015
0.07
0.02
0.08
0.03
0.09
0.04
0.1
0.07
0.09
0.04
0.08
0.03
0.07
0.02
0.06
0.01
0.055
0.005
0.05
0.0
0.04
-0.01
0.03
-0.02
0.02
-0.03
0.02
-0.03
0.02
-0.04
0
-0.05
-0.02
-0.07
-0.05
-0.1
0.05
0.25
0.45
0.55
0.67
1
0.65
0.4
0.3
0.15
0
-0.05
-0.15
-0.21
-0.27
-0.25
-0.22
-0.1
0
0
-0.03
0.02
-0.01
0.04
-0.01
0.04
-0.01
0.04
-0.05
0.045
0.0
0.05
0.05
0.0
0.055
0.005
0.06
0.01
0.08
0.03
0.15
0.1
0.175
0.125
0.18
0.13
0.185
0.135
0.2
0.19
0.14
0.16
0.11
0.14
0.09
0.12
0.07
0.02
]

No comments:

Post a Comment