fname = 'C:\Users\Joe\Google Drive\MAT598 Fourier\HW4'; %desktop %fname = '/Users/joesadow/Google Drive/MAT598 Fourier/HW4'; %mac %% Initiate and compute FFT warning off; f = @(t) exp(-t.^2./10).*(sin(2*t) + 2*cos(4*t) + 0.4.*sin(t).*sin(50.*t)); t = -7:0.1:7; j = 1:256; freqax = 2*pi.*j./256; y = f(2*pi.*j./256); %discrete version of signal yhat = fft(y); %DFT %% Filter M = [6 10 15 30]; %various m values P = []; %percent of coeff filtered out for l = 1:numel(M) m = M(l); filterout = m:255-m; yhatf = yhat; yhatf(filterout) = 0; f_fil = ifft(yhatf); perct = numel(yhatf(filterout))/numel(yhat); P = [P perct]; fig = figure; %generate and save filtered signal plots plot(j,y,'k',j,f_fil,'r') str = strcat({'f with filtered f (m = '}, num2str(m),')'); title(str); legend('Original Signal','Filtered Signal') xlabel('t') ylabel('f(t)') str2 = strcat(num2str(l)); saveas(fig, fullfile(fname, str2), 'epsc'); end fig = figure; %generate and save filtered DFT plots plot(freqax,abs(yhat),'*k',freqax,abs(yhatf),'*r') str = strcat({'DFT of f with filtered DFT (m = '}, num2str(m),')'); title(str); legend('Original DFT','Filtered DFT') xlabel('frequency') ylabel('fft') str2 = strcat(num2str(5)); saveas(fig, fullfile(fname, str2), 'epsc'); T = table(M',P','VariableNames',{'m','PercentCoeff'}); disp(T); %% Compress tol = [.5 1 10 20]; %various tolerances P = []; %percent of coeff filtered out Err = []; %relative error in L2 sense for l = 1:numel(tol) epsi = tol(l); yhatc = yhat; yhatc(find(yhat < epsi)) = 0; f_c = ifft(yhatc); perct = numel(yhatc(find(yhat < epsi)))/numel(yhatc); P = [P perct]; l2err = norm(yhat - yhatc,2)/norm(yhat,2); Err = [Err l2err]; fig = figure; %generate and save compressed signal plots plot(j,y,'k',j,f_c,'r') str = strcat({'f with compressed f with %'},num2str(100*perct),... ' coefficients taken out'); title(str); legend('Original Signal','Compressed Signal') xlabel('t') ylabel('f(t)') str2 = strcat(num2str(l+5)); saveas(fig, fullfile(fname, str2), 'epsc'); end T = table(tol',P',Err','VariableNames',{'Tolerance','PercentCoeff', 'L2CompressionError'}); disp(T);