Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- close all;
- f = 100;
- fs = 1000;
- T = 1;
- x = transpose(linspace(0,T,fs*T));
- y = sin(2*pi*f*x) + 0.01*sin(2*pi*2*f*x) + 0.25*sin(2*pi*4*f*x);
- %Make sure we have an even number of points;
- if (mod(length(y), 2) == 1)
- y = y(1:end-1,1:end);
- end
- n=length(y);
- %Build windows
- windows = ones(n,1);
- windows = [windows, hanning(n)];
- windows = [windows, hamming(n)];
- windows = [windows, blackman(n)];
- windows = [windows, blackmanharris(n)];
- windows = [windows, gausswin(n)];
- labels = {'Square'; 'Hanning'; 'Hamming'; 'Blackman'; 'Blackman-Harris'; 'Gaussian'};
- %Add Zero-Padding
- [n, winnum] = size(windows);
- data = repmat(y,1,winnum) .* windows;
- padlength = 16*n;
- padded_data = [zeros(padlength/2,winnum); data; zeros(padlength/2,winnum)];
- padded_windows = [zeros(padlength/2,winnum); windows; zeros(padlength/2,winnum)];
- p = fs/n; %precision
- %Assign memory
- data_spectrum = zeros(size(padded_data));
- window_spectrum = zeros(size(padded_windows));
- conv_spectrum = zeros([2*(n+padlength)-1 winnum]);
- figure;
- for i=1:winnum
- %Computes FFTs
- data_spectrum(1:end,i) = (abs(fftshift(fft(padded_data(1:end,i))))/length(y));
- window_spectrum(1:end,i) = (abs(fftshift(fft(padded_windows(1:end,i))))/length(padded_windows));
- conv_spectrum(1:end,i) = conv(window_spectrum(1:end,i),data_spectrum(1:end,i));
- %Draw plots
- subplot(2,3,i);
- hold on;
- faxis = linspace(-fs/2+p/2, fs/2-p/2,n+padlength);
- plot(faxis, 10*log(data_spectrum(1:end,i)))
- conv_n = length(conv_spectrum(:,i));
- idx = ceil(conv_n/2-conv_n/4)+1;
- plot(faxis, 10*log(conv_spectrum(idx:idx+length(data_spectrum(1:end,i))-1,i)));
- ylim([-200 0])
- title(labels{i});
- hold off;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement