Problems with Plotting discrete signals - Nyquist Sampling

7 views (last 30 days)
I created a program on Octave with the purpose of creating a continuous-time signal with noise in it, this noise was then supposed to be discretely sampled(shown as the orange lines fitting inside the sine wave below). This was supposed to be the expected output after completing the code.
https://i.stack.imgur.com/taOuP.png
However, while I did manage to get the same exact wave, what I did not get was the discrete sampling of the continuous-time signal, getting this as a result.
https://i.stack.imgur.com/3XbWe.png
I'm not sure as to why the lines did not fit in between the graph since I had applied the same Nyquist Theorem to them, the code below will better explain what I mean.
%Time Base
t = 0:0.001:1.8;
%Nyquist Frequencies
Fn1 = 1;
Fn2 = 6;
%Nyquist Rates
Fnr1 = 2*(Fn1);
Fnr2 = 2*(Fn2);
%Sampling Period
Sp1 = 5*(Fnr1);
Sp2 = 5*(Fnr2);
Ts1 = 1/(Sp1);
Ts2 = 1/(Sp2);
T1 = 1/(Fn1);
T2 = 1/(Fn2);
%Number of Samples
N1 = (T1/Ts1);
n1 = 0:1:N1-1;
N2 = (T2/Ts2);
n2 = 0:1:N2-1;
nTs1 = n1 * Ts1;
nTs2 = n2 * Ts2;
x_c = sin(2*pi*Fn1*nTs1);
x_c2 = sin(2*pi*Fn2*nTs2);
x_c1 = sin(2*pi*Fn1*t);
x_2 = sin(2*pi*Fn2*t);
signal = x_c1 + x_2;
ct = nTs1 + 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
I just want to know where I went wrong and how I could fix such a mistake.

Accepted Answer

Mathieu NOE
Mathieu NOE on 29 Oct 2020
seems i have already answered this question in another post !
%Time Base
Fs = 1000;
dt = 1/Fs;
t = 0:dt:1.8;
samples = length(t);
%Sine Frequencies
Fn1 = 1;
Fn2 = 6;
signal = sin(2*pi*Fn1*t) + 0.25*sin(2*pi*Fn2*t);
% decimated signal for tem plot
decim = 50; % decimation factor
ind = (1:decim:samples);
t2 = t(ind);
x2 = signal(ind);
%Second Part
h = stem(t2, x2, '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
  2 Comments
Joel Okanta
Joel Okanta on 9 Nov 2020
I used your code and made a few edits around it to get a similar output. But overall thank you for this response.
I'll look over the code more to understand it better and incorporate your techniques in my future coding projects. Thank you

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!