Advertisement
tsounakis

1.2 Signal Processing

Nov 4th, 2021
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | None | 0 0
  1. function one_2()
  2.     clc;
  3.     clear;
  4.    
  5.     global Fs;
  6.     Fs = 1000;
  7.    
  8.     samples = 200;
  9.  
  10.     t = 0:1/Fs:1/2*samples*1/Fs;
  11.     x_n = cos(125.*pi.*t).*cos(325.*pi.*t)
  12.     L = 200;
  13.    
  14.     plot_signal(x_n, "Amplitude", "Samples", "Sampled signal at f_s = 1 KHz sampling rate", false, NaN, 1);
  15.    
  16.     t = 0:1/Fs:samples*1/Fs;
  17.     x_n = cos(125.*pi.*t).*cos(325.*pi.*t)
  18.    
  19.     plot_spectrum(x_n, L, 2);
  20.     plot_spectrum(x_n, L/2, 3);
  21. end
  22.  
  23. function plot_signal(x, ylab, xlab, tl, isFreq, N, p)
  24.     global Fs;
  25.     subplot(3,1,p);
  26.     if (isFreq == true)
  27.         stem((-N/2:N/2-1)*Fs/N, x, 'b.-', 'LineWidth', 2, 'MarkerSize', 15)
  28.     else
  29.         stem(0:numel(x)-1, x, 'r.-', 'LineWidth', 2, 'MarkerSize', 15)
  30.     end
  31.     title(tl);
  32.     xlabel(xlab);
  33.     ylabel(ylab);
  34.     grid on;
  35. end
  36.  
  37. function plot_spectrum(x, N, p)
  38.     fx = fftshift(fft(x, N));
  39.     plot_signal(abs(fx), "|X(f)|", "Frequency (Hz)", "Spectrum of the x(n) signal, N = " + N, true, N, p);
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement