You are on page 1of 1

close all

clear all
clc

% Signal Parameters------------------------------
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 10001; % Length of signal
t = (-floor(L/2):1:floor(L/2))*T; % Time vector

% Signal Creation--------------------------------
S=sin(2*pi*20*t);
figure
subplot(3,1,1)
hold on
grid on
set(gca,'fontsize',25)
plot(t(1:100),S(1:100),'k','linewidth',2)
title('Signal')
xlabel('t (Seconds) ')
ylabel('X(t)')

% Transforming to frequency domain----------------


f = Fs*(-floor(L/2):floor(L/2))/L;
Y = fftshift(fft(S/L));

% Magnitude Plot----------------------------------
Mag_plot= abs(Y);
subplot(3,1,2)
hold on
grid on
set(gca,'fontsize',25)
plot(f,Mag_plot,'k','linewidth',2)
title('Magnitude Spectrum')
xlabel('f (Hz)')
ylabel('|X(f)|')
% Phase plot--------------------------------------
max_mag=max(Mag_plot);
Y(Mag_plot<(0.95*max_mag))=0;
Phase_plot= angle(Y);
subplot(3,1,3)
hold on
grid on
set(gca,'fontsize',25)
plot(f,Phase_plot/pi,'k','linewidth',2)
title('Phase Spectrum')
xlabel('f (Hz)')
ylabel('phase(pi)')

You might also like