#include <stdio.h> #include <math.h> #include <chplot.h> #include <numeric.h> #define N 256 /* total sample points */ #define M 4 /* sample time, 4 seconds */ int main() { int i; array double t[N], f[N], E[N/2], W[N/2+1],y[2][N/2+1]; array double complex F[N], Fp[N/2+1], Fa[N/2+1]; double Ts, Ws; linspace(t, 0, M); f = 9*exp(-9*t); Ts = t[1] - t[0]; Ws = 2*M_PI/Ts; fft(F,f); linspace(W,0,N/2); W = W*Ws/N; for (i=0; i<N/2+1; i++) { Fp[i] = F[i]*Ts; Fa[i] = 9.0/(9+W[i]*complex(0,1)); y[0][i] = abs(Fp[i]); y[1][i] = abs(Fa[i]); } plotxy(W,y,"FFT example","Frequency, Rad/s","|F(w)|, dB"); }