vcd fft(const vcd& a, bool invert=false) { int n = a.size(); vcd A(a.size()); for(int i=0, j=0; i<a.size(); ++i) { A[j] = a[i]; int bit = n>>1; for(; j>=bit && bit>1; bit>>=1) j -= bit; j += bit; } for(int s=2; s<=n; s<<=1) { double ang = 2 * pi / s * (invert ? -1 : 1); cd ws(cos(ang), sin(ang)); for(int j=0; j<n; j+=s) { cd w=1; for(int k=0; k<s/2; k++) { cd u = A[j+k]; cd t = A[j+s/2+k]; A[j+k] = u + w * t; A[j+s/2+k] = u - w * t; w *= ws; } } } if(invert) foreach(i,A) A[i] /= n; return A; }
void fft_inv(vcd& a) { fft(a); for (int i = 0; i < N; ++i) { a[i] /= N; } //now we have the identity matrix from the bottom to top reverse(a.begin() + 1, a.end()); }
void fft(vcd &a, bool invert) { int n = a.size(); if (n == 1) return; vcd a0, a1; a0.resize(n / 2); a1.resize(n / 2); for (int i = 0, j = 0; i < n; i += 2, ++j) { a0[j] = a[i]; a1[j] = a[i + 1]; } fft(a0, invert); fft(a1, invert); double ang = 2 * 3.14159265359 / n * (invert ? -1 : 1); complex<double> w(1), wn(cos(ang), sin(ang)); for (int i = 0; i < n / 2; ++i) { a[i] = a0[i] + (w * a1[i]); a[i + n / 2] = a0[i] - (w * a1[i]); w = w * wn; } if (invert) { for (int i = 0; i < n; ++i) a[i] /= 2; } }
double rms(vcd v) { int num = v.size(); double temp = 0.0; vd temp_v(num); for (int z=0; z<num; z++) {temp_v[z]=pow(abs(v[z]),2);} // t1=abs(v)^2 temp = accumulate(&temp_v[0],&temp_v[num-1],temp_v[num-1])/(1.0*num); // t2=mean(t1) temp = sqrt(temp); // rms=sqrt(t2) return temp; }
vcd fft(const vcd & v, bool inverse = false) { static const double PI = acos(-1.0); int n = v.size(), k = 0, high1 = -1; while ((1 << k) < n) k++; std::vector<int> rev(n); rev[0] = 0; for (int i = 1; i < n; i++) { if ((i & (i - 1)) == 0) high1++; rev[i] = rev[i ^ (1 << high1)]; rev[i] |= (1 << (k - high1 - 1)); } vcd roots(n), res(n); for (int i = 0; i < n; i++) { double alpha = 2 * PI * i / n; roots[i] = cd(cos(alpha), sin(alpha)); } for (int i = 0; i < n; i++) res[i] = v[rev[i]]; for (int len = 1; len < n; len <<= 1) { vcd tmp(n); int rstep = roots.size() / (len * 2); for (int pdest = 0; pdest < n;) { int p1 = pdest; for (int i = 0; i < len; i++) { cd val = roots[i * rstep] * res[p1 + len]; tmp[pdest] = res[p1] + val; tmp[pdest + len] = res[p1] - val; pdest++, p1++; } pdest += len; } res.swap(tmp); } if (inverse) { for (int i = 0; i < (int)res.size(); i++) res[i] /= v.size(); std::reverse(res.begin() + 1, res.end()); } return res; }
void init() { int hb = -1; rev.assign(N, 0); _root.assign(N, 0); for (int i = 1; i < N; ++i) { if ((i & (i - 1)) == 0) { ++hb; } rev[i] = rev[i ^ (1 << hb)]; rev[i] |= (1 << (LOG - hb - 1)); } for (int i = 0; i < N; ++i) { ld angle = 2.0 * PI * i / (1.0 * N); _root[i] = cd(cosl(angle), sinl(angle)); } }
void FFT(vcd & IN,vcd & OUT,int n,bool perform_inv){ bit_rev(IN,OUT,n); for(int s = 1,lg = len(n) - 1;s <= lg;s++){ int m = (1 << s); cd wm = cd(cos(2.0*PI/m),sin(2.0*PI/m)); for(int k = 0;k < n;k += m){ cd w = cd(1.0,0.0); for(int j = 0;j < m/2;j++){ cd t = w*OUT[k + j + m/2]; cd u = OUT[k + j]; OUT[k + j] = u + t; OUT[k + j + m/2] = u - t; if(perform_inv) w /= wm; else w *= wm; } } } if(perform_inv) for(int i = 0;i < OUT.size();i++) OUT[i] /= n; }
void display(vi E,vcd st,vi du,vi dc,vi sym,vcd s,vcd n,vcd r,vi sym_est,vi sym_err,vi destu,vi destc,vi berru,vi berrc) { cout << endl; cout<<" Symbol Table: "; for(int n=0;n<st.size();n++){cout<< st[n]<<" ";} cout<<endl; //cout<<" RMS: " << RMS <<endl; //cout << endl; //cout<<" Signal TX: "; for (int z=0;z<s.size();z++){cout<<s[z]<<" ";} cout<<endl; //cout<<" Noise: "; for (int z=0;z<n.size();z++){cout<<n[z]<<" ";} cout<<endl; //cout<<" Signal RX: "; for (int z=0;z<r.size();z++){cout<<r[z]<<" ";} cout<<endl; cout << endl; cout<<" Sym. Ind. TX: "; for(int z=0;z<sym.size();z++){cout<<sym[z]<<" ";} cout<<endl; cout<<" Sym. Ind. Est.: "; for(int z=0;z<sym_est.size();z++){cout<<sym_est[z]<<" ";} cout<<endl; cout<<" Sym. Error: "; for(int z=0;z<sym_err.size();z++){cout<<sym_err[z]<<" ";} cout<<endl; cout << endl; cout<<" Data (Coded): "; for(int z=0;z<dc.size();z++){cout<<dc[z];} cout<<endl; cout<<" Data Est. (Coded): "; for(int z=0;z<destc.size();z++){cout<<destc[z];} cout<<endl; cout<<" Bit Error (Coded): "; for(int z=0;z<berrc.size();z++){cout<<berrc[z];} cout<<endl; cout << endl; cout<<" Data (Uncoded): "; for(int z=0;z<du.size();z++){cout<<du[z];} cout<<endl; cout<<"Data Est. (Uncoded): "; for(int z=0;z<destu.size();z++){cout<<destu[z];} cout<<endl; cout<<"Bit Error (Uncoded): "; for(int z=0;z<berru.size();z++){cout<<berru[z];} cout<<endl; cout << endl; }