Skip to content

Instantly share code, notes, and snippets.

@ij96
Created March 9, 2018 17:37
Show Gist options
  • Select an option

  • Save ij96/9ff282359a5e9dd4974813f2b87f7770 to your computer and use it in GitHub Desktop.

Select an option

Save ij96/9ff282359a5e9dd4974813f2b87f7770 to your computer and use it in GitHub Desktop.
MATLAB script to demonstrate the beating effect when an analogue signal is sampled
% demonstrate the beating effect when an analogue signal is sampled
% more on: https://en.wikipedia.org/wiki/Beat_(acoustics)
clc; close all;
%//////////////////// parameters //////////////////////////////////////////
samp_freq = 8000; % sampling frequency
input_freq = 3800; % input sine wave frequency
num_of_cycles = 15; % number of input cycles to display on graph
x_accu_reso = 100; % input resolution: number per sample points on input
% per cycle
% - need to be large to see accurate representation
% of the input on graph
%//////////////////// calculation /////////////////////////////////////////
x_samp_reso = samp_freq / input_freq; % sampled resolution
% total number of samples points in graph for sampled
num_of_samp_samp = floor(x_samp_reso * num_of_cycles);
% total number of samples points in graph for input
num_of_samp_accu = floor(num_of_samp_samp * x_accu_reso / x_samp_reso);
x_samp = 1:1:num_of_samp_samp; % step range for sampled
x_accu = 1:1:num_of_samp_accu; % step range for input
y_samp = zeros(num_of_samp_samp,0); % sampled values initialised to 0
y_accu = zeros(num_of_samp_accu,0); % input values initialised to 0
% calculate input values
for i = 1:1:num_of_samp_accu
y_accu(i) = sin(i*(2*pi)/x_accu_reso);
end
% calculate sampled values
for i = 1:1:num_of_samp_samp
y_samp(i) = sin(i*(2*pi)/x_samp_reso);
end
%//////////////////// draw figure /////////////////////////////////////////
figure;
hold on;
grid on;
plot(x_accu / x_accu_reso, y_accu);
plot(x_samp / x_samp_reso, y_samp, '-*');
title(input_freq + "Hz sine wave sampled at " + samp_freq + "Hz");
ylabel("Amplitude (V)");
xlabel("Time (Cycle)");
legend("input", "sampled");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment