Advertisement
Guest User

Untitled

a guest
Dec 31st, 2016
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.70 KB | None | 0 0
  1. close all;
  2. f = 100;
  3. fs = 1000;
  4. T = 1;
  5. x = transpose(linspace(0,T,fs*T));
  6. y = sin(2*pi*f*x) + 0.01*sin(2*pi*2*f*x) + 0.25*sin(2*pi*4*f*x);
  7. %Make sure we have an even number of points;
  8. if (mod(length(y), 2) == 1)
  9.     y = y(1:end-1,1:end);
  10. end
  11.  
  12. n=length(y);
  13.  
  14. %Build windows
  15. windows = ones(n,1);
  16. windows = [windows, hanning(n)];
  17. windows = [windows, hamming(n)];
  18. windows = [windows, blackman(n)];
  19. windows = [windows, blackmanharris(n)];
  20. windows = [windows, gausswin(n)];
  21. labels = {'Square'; 'Hanning'; 'Hamming'; 'Blackman'; 'Blackman-Harris'; 'Gaussian'};
  22.  
  23. %Add Zero-Padding
  24. [n, winnum] = size(windows);
  25. data = repmat(y,1,winnum) .* windows;
  26. padlength = 16*n;
  27. padded_data = [zeros(padlength/2,winnum); data; zeros(padlength/2,winnum)];
  28. padded_windows = [zeros(padlength/2,winnum); windows; zeros(padlength/2,winnum)];
  29.  
  30. p = fs/n;   %precision
  31. %Assign memory
  32. data_spectrum = zeros(size(padded_data));
  33. window_spectrum = zeros(size(padded_windows));
  34. conv_spectrum = zeros([2*(n+padlength)-1 winnum]);
  35.  
  36. figure;
  37.  
  38. for i=1:winnum
  39.     %Computes FFTs
  40.     data_spectrum(1:end,i) = (abs(fftshift(fft(padded_data(1:end,i))))/length(y));
  41.     window_spectrum(1:end,i) = (abs(fftshift(fft(padded_windows(1:end,i))))/length(padded_windows));
  42.     conv_spectrum(1:end,i) = conv(window_spectrum(1:end,i),data_spectrum(1:end,i));
  43.    
  44.     %Draw plots
  45.     subplot(2,3,i);
  46.     hold on;
  47.     faxis = linspace(-fs/2+p/2, fs/2-p/2,n+padlength);
  48.     plot(faxis, 10*log(data_spectrum(1:end,i)))
  49.     conv_n = length(conv_spectrum(:,i));
  50.     idx = ceil(conv_n/2-conv_n/4)+1;
  51.     plot(faxis, 10*log(conv_spectrum(idx:idx+length(data_spectrum(1:end,i))-1,i)));
  52.     ylim([-200 0])
  53.     title(labels{i});
  54.     hold off;
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement