%hamming1.m clc clear all close all display('Running hamming1.m'); % %window width parameter 0 <= p < 0.5 p=0.1; %signal t = 0:0.015:10 ; % Time vector x = sin(20*t); % Signal %rectangular window N=length(t); l=1:N; for k=1:N rect(k) = 1; if k < p*N rect(k)=0; end; if k > (1-p)*N rect(k)=0; end; end %fft of rectangular window fftrect=fftshift(fft(rect)); mrectdb=20*log(abs(fftrect)); f = (0:length(x)-1)/length(x)-0.5; % Frequency vector figure; subplot(2,1,1); plot(t,rect); title('Rectangular window in time domain'); axis([0 10 0 2]); subplot(2,1,2); plot(f,mrectdb); title('Rectangular window in freq domain'); axis([-0.1 0.1 -50 150]); display('rectangular window...hit return to continue'); pause; %filter using rectangular window %FFT of signal %window the signal for i=1:N x(i) = x(i)*rect(i); end %FFT %fftx0 = fft(x); % Compute DFT of x fftx=fftshift(fft(x)); mx = abs(fftx); px = unwrap(angle(fftx)); % Magnitude and phase mxdb=20*log(mx); %plot signal figure; subplot(2,1,1) plot(t,x), title('signal*rectangle'), axis([0 10 -1 1]); subplot(2,1,2) plot(f,mxdb); title('signal*rectangle - frequencx domain (magnitude)'), axis([-0.5 0.5 -50 150]); display('DFT using rectangular window...hit return to continue'); pause; %hamming window for k=1:N ham(k) = 0.54 -0.46*cos(2*pi*(k-p*N)/((1-2*p)*N-1)); if k < p*N ham(k) = 0; end if k > (1-p)*N ham(k)=0; end; end %fft of hamming window fftham=fftshift(fft(ham)); mhamdb=20*log(abs(fftham)); figure; subplot(2,1,1); plot(t,ham); title('Hamming window in time domain'); axis([0 10 0 2]); subplot(2,1,2); plot(f,mhamdb); title('Hamming window in freq domain'); axis([-0.1 0.1 -50 150]); display('Hamming window...hit return to continue'); pause; %filter using hamming window %window the signal for i=1:N xh(i) = x(i)*ham(i); end fftxh0 = fft(xh); % Compute DFT of xh fftxh=fftshift(fftxh0); mxh = abs(fftxh); pxh = unwrap(angle(fftxh)); % Magnitude and phase mxhdb=20*log(mxh); %plot ham windowed signal figure; subplot(2,1,1) plot(t,xh), title('signal*hamming'), axis([0 10 -1 1]); subplot(2,1,2) plot(f,mxhdb); title('signal*hamming - frequencx domain (magnitude)'), axis([-0.5 0.5 -50 150]); display('DFT using Hamming window...'); display('...done');