Nyquist/Shannon Theorem MATLAB Code suffering from minor issues, don't know where I went wrong?
54 views (last 30 days)
Show older comments
A few months ago I asked a similar question as to how I could properly create nyquist/shannon graphs on MATLAB, and fortunately I got help from someone here. However, in those few months I managed to learn a lot and tried this task again with this new knowledge and manipulation of matlab tools. When I gave it a try, I only ended up getting a half/small sampled graph instead the whole thing.
What I did was what I learned about the Nyquist theorem, which was that:
Nyquist Frequency is the highest frequency, therefore this nyquist frequency gets doubled to get the nyquist rate. This nyquist rate then gets multiplied by 5 to get the sampling frequency and finally is divided over one to get the sampling period(1/F = T). The code below shows perfectly how I followed this process.
%Time Base
t = 0:0.001:2;
%Nyquist Frequencies
Fn1 = 1;
Fn2 = 6;
%Nyquist Rates
Fnr2 = 2*(Fn2);
%Sampling Period
Sp2 = 5*(Fnr2);
Ts2 = 1/(Sp2);
T2 = 1/(Fn2);
%Number of Samples
N2 = (T2/Ts2);
n2 = 0:1:N2-1;
nTs2 = n2 * Ts2;
x_c = sin(2*pi*Fn1*nTs2);
x_c2 = sin(2*pi*Fn2*nTs2);
signal = sin(2*pi*Fn1*t) + sin(2*pi*Fn2*t);
ct = nTs2;
nqsignal = x_c + x_c2;
%Second Part
h = stem(ct, nqsignal, 'linewidth', 2);
hold
plot(t, signal, 'linewidth', 2)
lgd = legend('Discrete Data', 'Continuous Data');
set (lgd, "fontsize", 12)
set(gca,'XTick',[0:0.2:1.8]);
set(gca,'YTick',[-2:0.5:2]);
title('Time vs Magnitude','fontweight','bold','fontsize',16);
xlabel('Time(s)','fontweight','bold','fontsize',14)
ylabel('Magnitude','fontweight','bold','fontsize',14)
grid
My Output from this code:
What I'm supposed to get:
I later found out that if I added more to the Number of samples (which is the variable N2), I could actually get the same output depending on whatever random number I add to it(e.g. I could do N2+50 rather than N2-1 and get the same output as this graph above) instead of doing "N2-1". Why that works, i don't know, but is there any other way I could get this same answer/output without having to make random and unexplainable modifcations to the Number of Samples? I have tried reducing the number of steps to get to the number of samples but that only increases the number of samples in the given area, without extending it.
Thank you in advance, and please leave a comment if I was not clear enough in this question
5 Comments
Answers (0)
See Also
Categories
Find more on Get Started with Signal Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!