ME 565 HW 4 Solutions Winter 2017 clear all; close all; clc %%Exercise 4.1 %read in the image A= imread('recorder','jpg'); Make image black and white Abw2=rgb2gray(A); [nx,ny]=size(abw2); Compute the FFT of our image using fft2 disp('doing FFT analysis for sparsity check...') Doing FFT analysis for sparsity check... At=fft2(Abw2); F = log(abs(fftshift(at))+1); %scaling for plot F = mat2gray(f); % Use mat2gray to scale the image between 0 and 1 % figure(3) % imshow(f,[]); % Display the fft coefficients % taking the fft At=fft2(Abw2); %%%what we want is to keep exactly a certain percentage of the coefficients
count_pic=2; trials=[10, 1]; count=nx*ny; %the total number of pixels figure(2), subplot(1,3,1), imshow(abw2) title('original image','fontsize',12) figure (3), subplot(1,3,1),surf(double(abw2)); title('original image','fontsize',12) for ii=1:length(trials) percentage=trials(ii); indceil= ceil(0.01*percentage*count); %the number of coefficients for the final image %order the coefficients from largest to smallest and keep the top x %zero out the indices that are not in this top x% %reshape At; reshaping does not change the intrinsic order of the elements %if we use linear indexes At_rshp=reshape(At,count,1); %sort descending [At_rshp_srt, topindices]= sort(abs(at_rshp),'descend'); %the indices of top x% of the Fourier Coefficients idx_lookup= topindices(1:indceil); % check(ii)=length(idx_lookup); %I make a matrix At_reduced the size of my original At and then subsitute in the %elements that correspond to the top x% of the Fourier Coefficients At_reduced=zeros(size(At)); At_reduced(idx_lookup)=At(idx_lookup); Alow=uint8(ifft2(At_reduced)); %L2 norm comparison disp('l2 norms...') image_error=abw2-alow; L2_norm_error=norm(double(image_error),2) L2_norm_fft_orig=norm(At,2) L2_norm_fft_comp=norm( At_reduced,2) norm_low=norm(double(alow),2) %computing the L2 norm of the new image figure(2), subplot(1,3,count_pic), imshow(alow); drawnow title([num2str(percentage) '% of FFT basis'],'fontsize',12) figure(3), subplot(1,3,count_pic), surf(double(alow)); drawnow title([num2str(percentage) '% of FFT basis'],'fontsize',12) count_pic=count_pic+1; end clear idx_lookup L2 norms... L2_norm_error = 401.7412 L2_norm_fft_orig = 1.4359e+08 L2_norm_fft_comp = 1.4359e+08 norm_low = 1.0714e+05
L2 norms... L2_norm_error = 3.0244e+03 L2_norm_fft_orig = 1.4359e+08 L2_norm_fft_comp = 1.4358e+08 norm_low = 1.0709e+05
The L2 norm of the error between the original image and the 10% case is smaller than for the 1% case. The L2 norm of the fourier coefficients for the 10% case is also closer to the L2 norm of the fourier coefficients of the original image indicated that it is a closer match to the original. This matches our assessment if we view the 10% and 1% images and compare these to the orignal image.
Exercise 4-2 clear all; close all; clc vars={'rush','fs'}; load ('r2112.mat',vars{:}) disp('play the original signal...') Play the original signal... sound(rush, FS); pause clc %Compute FFT of signal N=length(rush); FFT=fft(rush, N); %Compute PSD of signal and plot it along with the spectrogram figure PSD_clean=FFT.*conj(FFT)/N; freq=(fs/n)*(1:n); L=1:floor(N/2); subplot(211) plot(freq(l), 10*log10(PSD_clean(L))) xlabel('frequency (Hz)') title('power Spectral Density -original Signal')
subplot(212) spectrogram (rush, 1000, 500, 1000, FS, 'yaxis') title ('Spectrogram of original signal') %Prefiltered signal analysis vars_noisy = {'rushnoisy'}; load ('r2112noisy.mat',vars_noisy{:}); %Listen to the noisy sample disp ('Play corrrupted signal...') Play corrrupted signal... sound(rushnoisy, FS); pause clc n=length(rushnoisy); FFT_noisy=fft(rushnoisy,n); %Plot PSD and spectrogram of noisy signal figure PSD_noisy=FFT_noisy.*conj(FFT_noisy)/n; freq_n=fs/n*(1:n); subplot(221)
plot(freq_n(l), 10*log10(PSD_noisy(L))) xlabel('frequency (Hz)') title('psd-noisy Signal') subplot(222) spectrogram (rushnoisy, 1000, 500, 1000, FS, 'yaxis') title ('Spectrogram of noisy signal') %Filtering and reconstuction of signal FFT_filtered=FFT_noisy; FFT_filtered(n/4:n)=0; %removing last 3/4 of Fourier components clean_signal =2*real(ifft(FFT_filtered)); disp('playing filtered signal...') Playing filtered signal... sound(clean_signal, FS) pause clc disp('compare to original...') Compare to original... sound(rush, FS) pause clc n_cleansig=length(clean_signal); FFT_cleansig=fft(clean_signal, n_cleansig); PSD_cleansig=FFT_cleansig.*conj(FFT_cleansig)/n_cleansig; freq_clean=fs/(n_cleansig)*(1:n_cleansig); subplot(223) plot(freq_clean(l),10*log10(psd_cleansig(l))) xlabel('frequency (Hz)') title('psd-filtered signal') subplot(224) spectrogram (clean_signal, 1000, 500, 1000, FS, 'yaxis') title ('Spectrogram of cleaned signal')
The above shows that we have successfully removed the high frequency contamination (as well as the high frequency components above 10000 Hz in the original signal)