% % lect11c.m -- code for FFT -- noise filtering % %- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - % set grids L= pi; N = 256; h = 2*L/N; x = 0:h:2*L-h; n = -N/2:1:N/2-1; % clean & dirty functions f = sin(7*pi*x/L); f1 = f + 0.5*randn(1,N); % fft filtering c = fftshift(fft(f1))/N; filt = (1-tanh(2*(n-10))).*(1+tanh(2*(n+10)))/4; f2 = ifft(fftshift(c.*filt)*N); % plot figure(1);clf subplot(2,1,1) plot(x,f1) axis([0 2*L -2 2]) title('\bf noisy wave') xlabel('\bf x-axis'); ylabel('\bf y-axis'); subplot(2,1,2) plot(x,real(f),'b--') hold on plot(x,real(f2),'r') axis([0 2*L -2 2]) title('\bf original (dashed) vs filtered (solid) waves') xlabel('\bf x-axis'); ylabel('\bf y-axis'); figure(2);clf subplot(2,1,1) plot(n,abs(c),'x') hold on plot(n,filt) axis([-N/2 N/2 0 1.1]) title('\bf noisy spectrum & filter profile') xlabel('\bf n-axis'); ylabel('\bf |c_n|-axis'); subplot(2,1,2) plot(n,abs(c.*filt),'x') axis([-N/2 N/2 0 1.1]) title('\bf filtered spectrum') xlabel('\bf n-axis'); ylabel('\bf |c_n|-axis');