#include // to make sure I get 8-byte integers #include #include #include #include using namespace std; int main() { ofstream outfil("output.txt"); int64_t cic[4],resold[4]; // cic[i] and resold[i] are the sums of the cic stages and the sums 1024 samples earlier // I use int64_t to be sure that 64-bit integers are used. you can use long also // typically, long is typecast to int64_t but that is not garantueed double result=0; // the result after 1024*n steps int64_t res; int input; int time=0; double rms[2]; // the rms can be calculated for (int i=0; i<4; i++) cic[i]=resold[i]=0; rms[0]=rms[1]=0; int totval=0; // in c++, cin returns 0 if the operation did not go right. while (cin >> input) { time ++; cic[0]+=input; for (int i=0; i<3; i++) cic[i+1]+=cic[i]; if (time%1024==0) { // every 1024 steps, decimate the results. check the values of the // 4-stage cic filters and the values of the previous steps int64_t res=cic[3]; for (int j=0; j<4; j++) { int64_t delay=resold[j]; // store the previous result in delay resold[j]=res; // update the result with the current value of the result res -= delay; // subtract the previous value from the result } result=res/pow(1024,4); // normalize the double-precision result // the true signal equals 2.7 sin(17t) double sig=2.7*sin(17*time/1024000.0); outfil << setprecision(9) << time/1024000.0 << " " << sig << " " << result << " " << input << endl; if (time/1024>1) { rms[1]+=pow(input-sig,2); sig=2.7*sin(17*(time-2048)/1024000.0); rms[0]+=pow(result-sig,2); totval++; } } } cout << "RMS of the input minus the signal : " << sqrt(rms[1]/totval) << " RMS of the CIC-filtered result : " << sqrt(rms[0]/totval) << endl; return 0; }