Skip to content

Instantly share code, notes, and snippets.

@rasgo-cc
Created September 13, 2015 01:39
Show Gist options
  • Save rasgo-cc/590df02cd38fb421e74d to your computer and use it in GitHub Desktop.
Save rasgo-cc/590df02cd38fb421e74d to your computer and use it in GitHub Desktop.
Comparing a perfect square wave with a real and imperfect one
clear all
close all
clc
pkg load signal;
Ns = 4096;
t_max = 0.1;
Ts = t_max / 4096;
Fs = 1/Ts;
t = linspace(0, t_max, Ns);
f = 50;
xin_perfect = square(2*pi*f*t);
figure(1);
plot(t,xin_perfect, 'LineWidth',1)
grid on
title("A Perfect Square Wave")
ylabel("Amplituude")
xlabel("Time (s)")
xin = 0 * t;
for k = [1:8]
xin += sin(2*pi*(2*k-1)*f*t)/(2*k-1);
end
xin = 4*xin/pi;
figure(2);
subplot(2,1,1);
plot(t,xin,'LineWidth',1)
title("Real/Imperfect Square Wave (sum of 8 sinusoids)")
ylabel("Amplituude")
xlabel("Time (s)")
grid on
Nfft = Ns*4;
Xf = abs(fft(xin, Nfft));
subplot(2,1,2);
f_ = linspace(0,Fs,Nfft);
plot(f_, Xf, 'LineWidth',1)
xlim([1,1000])
title("Frequency Spectrum")
xlabel("Frequency (Hz)")
grid on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment