Not sure of how I can sample this Nyquist wave? Combining data problems

4 views (last 30 days)
I was given a task of creating a continuous time signal consisting of two different sine waves added together, with this new wave then being sampled after its creation. I managed to complete the first part with ease by making two different sets of data and combining them to make a new sine. Though there came an issue with it, as I could not properl use the stem function to discretely "sample" the data.
This is what I got for my initial output:
As you can see, while the wave did end up fine in the end, discrete data was all over the place as you can see with the blue lines not fitting at all into the wave.
This is what I was supposed to get for my output:
Over in this image, you can see that the lines fit; the data is being correctly sampled discretely.
This is the code I used to get my intial output
%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;
N2 = (T2/Ts2);
n2 = 0:1:N2;
nTs1 = n1 * Ts1;
nTs2 = n2 * Ts2;
x_c = sin(2*pi*Fn1*nTs1);
x_c1 = sin(2*pi*Fn1*t);
x_c2 = sin(2*pi*Fn2*nTs2);
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
  2 Comments
Mathieu NOE
Mathieu NOE on 26 Oct 2020
hello
if I understand well from the attached picture, you just need to plot the original continous sine wave and it's sampled version
your first explanation is a bit confusing, I was thinking you have 2 different signals and you need to make a combination of the two... it's not what I guess from the pictures.
Joel Okanta
Joel Okanta on 28 Oct 2020
To break it down what i'm trying to do is to get the same result as the expected output just that when I did mine, I got some messy graph with the sampling lines not fitting inside the sine waves. My question is, how do i fix my code to get a better output?

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 29 Oct 2020
hi
to be honest with you, I rewrote the entire code, simply because the idea is to have one signal at Fs that will be then decimated to show the sampling .
so here is it - enjoy !
%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
  7 Comments
Mathieu NOE
Mathieu NOE on 22 Jan 2021
hello Joel
the sampling theory (Shannon theorem) states that the highest frequency of you signal must be below the Nyquist frequency but there are no fixed ratio between the two .
There is no need to add the signal frequencies to define the sampling frequency (otherwise a very complex signal like music or speech should be sampled at very high Fs !)
So you are free to choose F Nyq > 9 hz . Let's say 10 so Fs = 20 Hz min.
the multipliers (individual frequencies amplitudes) are completely free - and I don't think that it makes any difference for the purpose of this exercise.
Joel Okanta
Joel Okanta on 22 Jan 2021
I would like to know if I could gather the same results as you were getting using my previous code. I'm going to ask a new question about this and I want to know where i went wrong and how I can improve it
https://uk.mathworks.com/matlabcentral/answers/724413-nyquist-shannon-theorem-matlab-code-suffering-from-minor-issues-don-t-know-where-i-went-wrong

Sign in to comment.

More Answers (1)

Joel Okanta
Joel Okanta on 22 Jan 2021
I would like to know if I could gather the same results as you were getting using my previous code. I'm going to ask a new question about this and I want to know where i went wrong and how I can improve it

Community Treasure Hunt

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

Start Hunting!