// math for frequency response of 3-point moving average // #include #include #define PI (4*atan(1)) #define T (1/8000.0) // sampling period #define M 101 // H(w) = (1 + D + D*D) / 3 // D = cos(w*T) - j*sin(w*T) // D*D = cos(2*w*T) - j*sin(2*w*T) // Magnitude of H(w) is square-root of sum of squares of real and imaginary parts // double H(double w) { double r = (1 + cos(w*T) + cos(2*w*T))/3, i = (-sin(w*T) - sin(2*w*T))/3; return sqrt( r*r + i*i); } int main( void) { // Compute and plot magnitude of H(w) using linear scale f = 0 to 4000 // double fmin = 0, fmax = 4000, df = (fmax-fmin)/(M-1), f; for( int i = 0; i < M; ++i) { f = fmin + i*df; printf( "%g %g\n", f, H(2*PI*f) ); } return 0; }