Basic Sampling question involving sampling rate and graphing
Show older comments
For a labI was given this code
t=0:1/2000:.02;
x=sin(2*pi*60*t);
t240=0:1/240:.02;
n240=0:length(t240)-1;
x240=sin(2*pi*60/240*n240);
stem(n240,x240,'filled','b','LineWidth',2);
axis([0 4.8 -1 1]);
and was to put that into matlab and display the sampling and function which works fine. We then needed to make this into a function of x=Asin(omega*t) where f = 10000Hz, and find the sampling at rates of 5k, 10k, and 20, 100k. However changing the values of the sampling frequency and the function frequency gives me a graph that is not at all similar and I have no idea what I need to fix or change to get the graph to look like the initial one.
=======================================
t=0:1/3300:.02;
x=sin(2*pi*10000*t);
t5000=0:1/5000:.02;
n5000=0:length(t5000)-1;
x5000=sin(2*pi*10000/5000*n5000);
stem(n5000,x5000,'filled','b','LineWidth',2);
=======================================
t=0:1/3300:.02;
x=sin(2*pi*10000*t);
t10000=0:1/10000:.02;
n10000=0:length(t10000)-1;
x10000=sin(2*pi*10000/10000*n10000);
stem(n10000,x10000,'filled','b','LineWidth',2);
========================================
that is what I changed the code too for 5k and 10k, i know those are both less than nyquist's sampling so it will result in not a good graph, but even with 20k+ the graph isnt similar to what it was before.
what I specifically dont understand is
how
t=0:1/2000:.02
; is time
what
n240=0:length(t240)-1; is
this
stem(n240,x240,'filled','b','LineWidth',2);
part of the code and what it does
how to get a proper sampling graph of 5k,10k,20k and100k, and what to change to get that
3 Comments
t=0:1/3300:.02;
x=sin(2*pi*10000*t);
"I know those are both less than nyquist's sampling ..."
In that case, you should know what you need to do to fix the problems. If you're undersampled, the results of fft are going to be reflective of that fact and can't be fixed after the fact (at least without knowing much and making fixups based on knowing what the signal actual was).
"I specifically dont understand is how t=0:1/2000:.02; is time"
Well, by convention (altho it's not mandatory) one begins a time vector at the origin which explains the first "0"
The rest is MATLAB syntax -- the colon construct consists of a triplet -- start : interval : end so the end is 0.02 time units (we'll assume seconds) or 0.02 * 1000 msec/sec --> 20 msec. The sample rate is the interval --. 1/2000 would be 1/2*(1/1000) or 1/2 msec sample interval or sampling at a rate of 2 kHz. That's fine when the signal being generated with that time series is one of 60 Hz as the first example; that's 2000/60 or a sample rate of 33X times the signal. You can get by with anything >2 by Nyquist, 3-4X is better with shorter time periods.
But, you raised the frequency in your example to 10,000 or 10kHz/60Hz = 166.666X the former frequency but only raised the sample frequency by 3300/2000 = 1.65X or the signal frequency went up by 100X more than the sampling frequency. Thus, you're undersampled of 10 kHz/3.3 kHz = 3.03. You've got to increase the sample rate to at least >20kHz and preferably to 30 kHz or greater.
Then, you don't need nearly as long a time series, either -- you'll get multiple cycles of 10 kHz in only a couple msec instead of needing 200 msec.
Think about that some...
Oh. The other "240" thing is just changing the sample rate by that amount and also the frequency is adjusted compensatingly as well...
dpb
on 2 Apr 2021
ADDENDUM
"That's fine when the signal being generated with that time series is one of 60 Hz as the first example; that's 2000/60 or a sample rate of 33X times the signal. You can get by with anything >2 by Nyquist, 3-4X is better with shorter time periods."
That is, >Nyquist by any amount will be enough to find the frequency content via FFT with a suitably-long record; that does NOT mean that the time series graph will look smooth; a sample rate of only slightly over Nyquist will still look like a triangle wave mostly, not a smooth sine even though there is enough frequency content in the resulting signal to compute the appropriate frequencies without aliasing.
Zachariah Brown
on 3 Apr 2021
Answers (1)
Sulaymon Eshkabilov
on 3 Apr 2021
Your code has a problem with the end time value to take into calculations and display of the calculated data points. Here are some corrected codes:
f1=240;
t=0:1/2000:.02;
%x=sin(2*pi*60*t);
t240=0:1/f1:.02;
n240=0:length(t240)-1;
x240=sin(2*pi*60/f1*n240);
figure
subplot(311)
stem(n240,x240,'filled','b','LineWidth',2);
axis([0 4.8 -1 1]);
%%
%t=0:1/2000:.02;
%x=sin(2*pi*60*t);
f1=240;
t240=0:1/f1:.02;
n240=0:length(t240)-1;
x240=sin(2*pi*f1*n240);
figure
subplot(311)
stem(n240,x240,'filled','b','LineWidth',2);
axis([0 4.8 -1 1]);
subplot(312)
f2=5000; % 5k
t5k=0:1/(f2*10):.0005;
n5k=0:length(t5k)-1;
x5k=sin(2*pi*10000/f2*n5k);
stem(n5k,x5k,'filled','b','LineWidth',2); shg
f3=1e4; % 10k
% t=0:1/(10*f3):.02;
% x=sin(2*pi*f3*t);
t10k=0:1/(10*f3):.0002;
n10k=0:length(t10k)-1;
x10k=sin(2*pi*1*n10k);
subplot(313)
stem(n10k,x10k,'filled','b','LineWidth',2); shg
Good luck.
1 Comment
Zachariah Brown
on 3 Apr 2021
Categories
Find more on Linear Prediction in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!