clear all; close all; % clear all variables and figures L=20; % define the computational domain [-L/2,L/2] p=8; % define the number of Fourier modes 2^p N=2^p; % define the number of Fourier modes 2^p dt=L/N; t= (-L/2):dt:(L/2 - dt); yfun=inline('exp( -1*t.^2 )'); %yfun=inline('exp( -1*t.^2 )+cos(t)'); %yfun=inline('1*ones(1,length(t))') y=yfun(t); FFTlist=fft(y); FFTlist_indices=0:(N-1) ; %utshift=fftshift(ut); % shift FFT figure subplot(131) set(gca,'FontSize',18) plot(t,y); xlabel('t'); ylabel('y'); subplot(132) set(gca,'FontSize',18) bar(FFTlist_indices,FFTlist); xlabel('FFTlistindices'); ylabel('FFTlist'); axis tight %%% NOW UN-TRANSFORM %%% y_recovered = ifft(FFTlist) ; subplot(133) set(gca,'FontSize',18) plot(t,y_recovered); xlabel('t'); ylabel('yrecovered'); %%% NOW INTERPRET IN TERMS OF ANGULAR FREQUENCIES K %%% k_list=2*pi/L*[0:(N/2 -1) (-N/2):-1]; figure subplot(121) set(gca,'FontSize',18) plot(t,y); xlabel('t'); ylabel('y'); subplot(122) set(gca,'FontSize',18) bar(k_list,FFTlist); xlabel('ang. freq. k'); ylabel('FFTlist'); axis tight %%% NOW INTERPRET IN TERMS OF TEMPORAL FREQUENCIES f %%% temporal_freq_list=k_list/(2*pi) ; figure subplot(121) set(gca,'FontSize',18) plot(t,y); xlabel('t'); ylabel('y'); subplot(122) set(gca,'FontSize',18) bar(temporal_freq_list,FFTlist); xlabel('temporal freq. f'); ylabel('FFTlist'); axis tight %%% Add some noise to y y_noisy=y+0.2*randn(size(y)); FFTlist_noisy=fft(y_noisy); figure subplot(121) set(gca,'FontSize',18) plot(t,y_noisy); xlabel('t'); ylabel('ynoisy'); subplot(122) set(gca,'FontSize',18) bar(temporal_freq_list,FFTlist_noisy); xlabel('temporal freq. f'); ylabel('FFTlistnoisy'); axis tight cutoff=.3 FFTlist_denoised=FFTlist_noisy; %denoise for j=1:length(FFTlist_noisy) if abs(temporal_freq_list(j))>=cutoff FFTlist_denoised(j)=0; end end %recover y denoised y_denoised=ifft(FFTlist_denoised); figure subplot(221) set(gca,'FontSize',18) plot(t,y_noisy); xlabel('t'); ylabel('ynoisy'); subplot(222) set(gca,'FontSize',18) bar(temporal_freq_list,FFTlist_noisy); xlabel('temporal freq. f'); ylabel('FFTlistnoisy'); axis tight subplot(223) set(gca,'FontSize',18) bar(temporal_freq_list,FFTlist_denoised); xlabel('temporal freq. f'); ylabel('FFTlistdenoised'); subplot(224) set(gca,'FontSize',18) plot(t,y_denoised); xlabel('t'); ylabel('ydenoised'); axis tight