Skip to content

Instantly share code, notes, and snippets.

@wolframalexa
Created April 29, 2020 03:23
Show Gist options
  • Save wolframalexa/3e42df8d575365695186a81f440771d5 to your computer and use it in GitHub Desktop.
Save wolframalexa/3e42df8d575365695186a81f440771d5 to your computer and use it in GitHub Desktop.
MATLAB code generating graphs for my clap-as-impulse blog post
clear; close all; clc
%% Alexa Jakob
% Code for graphs at alexajakob.com/blog%20posts/2020/04/23/clap-as-impulse.md
%% Dirac Delta Function
x = -10:10;
d = dirac(x);
idx = d == Inf;
d(idx) = 1; % set Inf to finite value so it shows up
stem(x,d)
title('The Dirac-Delta Function')
xlabel('n')
ylabel('\delta[n]')
axis([-10 10 -1 1.5])
%% Example of Impulse Response
[b,a] = butter(3, [0.2, 0.5]);
[h,t] = impz(b,a,30);
figure
stem(t,h)
title('Impulse response of a sample system (Butterworth Filter)')
xlabel('n')
ylabel('y[n]')
%% Example of a signal and its Fourier Transform
n = 0:50;
x1 = sin(0.2*n);
x2 = sin(0.5*n);
figure
subplot(2,1,1)
stem(n,x1)
title('Signal x1 in time')
xlabel('n')
ylabel('x1[n]')
subplot(2,1,2)
stem(n,x2)
title('Signal x2 in time')
xlabel('n')
ylabel('x2[n]')
figure
subplot(2,1,1)
ft1 = abs(fftshift(fft(x1)));
plot(n,ft1)
title('Fourier Transform of x1')
xlabel('f (Hz)')
ylabel('|P(t)|')
subplot(2,1,2)
ft2 = abs(fftshift(fft(x2)));
plot(n,ft2)
title('Fourier Transform of x2')
xlabel('f (Hz)')
ylabel('|P(t)|')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment