Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Sampling Theorem in MATLAB

Status
Not open for further replies.

BumbleBee09

Newbie level 1
Joined
Dec 31, 2009
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,307
Hi all,

I have just started out with my signals and systems course and as an exercise I am trying to do visualise the sampling theorem on MATLAB Hence I need to do something as simple as multiplying a continous signal with an impulse train. I am struggling to write the code . Please help me!!

I can plot the sine/cosine signal and the impulse train separately but cannot multiply them :(

% Cosine from -pi to pi
t1 = linspace(0, 6 , 1000);
x = cos( 2* pi * t);

%Impulse train
t2 = linspace ( 0,6,7 );
train = ones(1,t2);

% sampledsig = x.*train;

Now when I try to multiply them , I am always getting an error which is obvious since the vector size is different . Any help is greatly appreciated .

Thanks & Regards
 

Why not use the 'stem' command ?

Just use the vectors of x and t1 and type

stem(t1,x)

and you automatically get the plot of a time disrecte signal
that still has continous values.

Best Regards

Andi
 

BumbleBee09 said:
Hi all,

I have just started out with my signals and systems course and as an exercise I am trying to do visualise the sampling theorem on MATLAB Hence I need to do something as simple as multiplying a continous signal with an impulse train. I am struggling to write the code . Please help me!!

I can plot the sine/cosine signal and the impulse train separately but cannot multiply them :(

% Cosine from -pi to pi
t1 = linspace(0, 6 , 1000);
x = cos( 2* pi * t);

%Impulse train
t2 = linspace ( 0,6,7 );
train = ones(1,t2);

% sampledsig = x.*train;

Now when I try to multiply them , I am always getting an error which is obvious since the vector size is different . Any help is greatly appreciated .

Thanks & Regards


What is the frequency of the cosine wave?
 

Matlab is a discrete solver. When you write x=cos(2*pi*t1); (t1 not t, therefore f=1) you have already multiplied your cosine by an impulse train whose sampling rate is fs=6/1000...

If you want to see the meaning of the Nyquist theorem just keep increasing the frequency until it is higher than fs/2. What do you see?
 

well, Nyquist's theorem can still be verified. In fact, you will observe that since cos(2*pi*t1) is not bandlimited, therefore you will require a sampling frequency much greater than 2*fmax
 

Communications_Engineer said:
In fact, you will observe that since cos(2*pi*t1) is not bandlimited, therefore you will require a sampling frequency much greater than 2*fmax
Well, I disagree: cos(2*pi*f*t1) is the most bandlimited signal you can think of! and according to the Nyquist theorem, you only need to sample at fs>2*f.
Try:
t=linspace(0,1,32);
f=3; x=cos(2*pi*f*t);
plot(t,x)
and then
f=28; x=cos(2*pi*f*t);
plot(t,x)
what is the difference?
 

JoannesPaulus said:
Communications_Engineer said:
In fact, you will observe that since cos(2*pi*t1) is not bandlimited, therefore you will require a sampling frequency much greater than 2*fmax
Well, I disagree: cos(2*pi*f*t1) is the most bandlimited signal you can think of! and according to the Nyquist theorem, you only need to sample at fs>2*f.
Try:
t=linspace(0,1,32);
f=3; x=cos(2*pi*f*t);
plot(t,x)
and then
f=28; x=cos(2*pi*f*t);
plot(t,x)
what is the difference?

Question for you:

Don't you think that since the frequencies are different then you should see "two different signals" in time domain?

Try:

cos(2*pi*3*t) - cos(2*pi*28*t) (≈ 0)

Let me also tell you that the mistake you have done is where you define t.

You have a step of 1/31, it means that the sampling frequency is 31 Hz and fmax can only be ≈ 15 Hz, meaning that the second plot you get is an alias of 28 Hz and not the original 28 Hz
 

Communications_Engineer said:
Question for you:
Don't you think that since the frequencies are different then you should see "two different signals" in time domain?
No, not at all: it depends on the sampling rate! This is what the Nyquist theorem is telling you. If you want an alias free reconstruction of your signal, you need to sample with the proper rate. If you do not, you will not be able to reconstruct your signal correctly (in this case, you will not be able to tell whether the input signal has frequency f=3 or f=28...).
This was to show that the cosine is bandlimited an therefore you can sample it with a rate lower than its frequency (a process called undersampling).
The Nyquist theorem says that the sampling rate must be greater than the signal bandwidth and the cosine has the smallest possible bandwidth.
Communications_Engineer said:
Let me also tell you that the mistake you have done is where you define t.

You have a step of 1/31, it means that the sampling frequency is 31 Hz and fmax can only be ≈ 15 Hz, meaning that the second plot you get is an alias of 28 Hz and not the original 28 Hz
No mistake there, it as exactly what I wanted to show!

I hope this now clears the confusion.
 

JoannesPaulus said:
Communications_Engineer said:
Question for you:
Don't you think that since the frequencies are different then you should see "two different signals" in time domain?

This was to show that the cosine is bandlimited an therefore you can sample it with a rate lower than its frequency (a process called undersampling). :?:

The Nyquist theorem says that the sampling rate must be greater than the signal bandwidth and the cosine has the smallest possible bandwidth.


No mistake there, it as exactly what I wanted to show!

I hope this now clears the confusion.


Nope.


Look, look at the two signals in frequency domain, you'll observe the difference yourself
 

can you please show me how the two signals in frequency domain are different (using the same sampling rate)?
 

JoannesPaulus said:
can you please show me how the two signals in frequency domain are different (using the same sampling rate)?

Thanks for being patient

Have a look at this code. You seem to a smart guy and would not need explanation and can infer from the graphs


-----------------------------------------------------------------------------------
clear all; clc;
t=linspace(0,1,32);
f=3;
x1=cos(2*pi*f*t);
f=7;
x2=cos(2*pi*f*t);
f=28;
x3=cos(2*pi*f*t);
Fs = 32; T = 1/Fs; % values taken from your definition of t
X1 = fft(x1);
X2= fft(x2);
X3= fft(x3);
k = 0:length(t)-1;
f = k*Fs/length(X1);
plot(f,abs(X1))
title('Freq domain charac. for signal with f = 3 Hz')
figure,plot(f,abs(X2))
title('Freq domain charac. for signal with f = 7 Hz')
figure,plot(f,abs(X3))
title('Freq domain charac. for signal with f = 28 Hz')
--------------------------------------------------------------[/img]
 

How is Fig. 1 different from Fig. 3??
If you keep the same sampling rate (fs=31), f=3 and f=28 look no different! Isn't this what I have been saying all along?
Code:
figure(1)
plot(0:31,x1)
axis([0 31 -1 1]);
grid on
title('Time domain for signal with f=3Hz')
figure(3)
plot(0:31,x3)
axis([0 31 -1 1]);
grid on
title('Time domain for signal with f=28Hz')
Communications_Engineer said:
where is the option of displaying pictures here? Do you know?
I believe it has to be smaller than a certain size but I am not sure (I never tried).
 

JoannesPaulus said:
How is Fig. 1 different from Fig. 3??
If you keep the same sampling rate (fs=31), f=3 and f=28 look no different! Isn't this what I have been saying all along?

Yes they are same, but you should see that since the sampling rate is small, you don't get the 28 Hz frequency component showing up, even though it should have.

A 3 Hz signal shows up (an alias of 28 Hz), so the result is incorrect.

Its like this, you are observing the following alias,

31 - 1 * 28 = 3 Hz

or generally,

F_alias = f ± k.Fs

where

f = frequency of desired (continuous-time signal that you want to sample)
k = 1 to inf
Fs = sampling frequency

There would be other aliases too, its just that you can't observe them since the frequency axis is limited to 0-Fs

I suggest you read Rick Lyons book on Digital Signal Processing
 

Let's start from the bottom:
Communications_Engineer said:
I suggest you read Rick Lyons book on Digital Signal Processing
I have about 20 years of experience in the field...

Communications_Engineer said:
JoannesPaulus said:
How is Fig. 1 different from Fig. 3??
If you keep the same sampling rate (fs=31), f=3 and f=28 look no different! Isn't this what I have been saying all along?

Yes they are same, but you should see that since the sampling rate is small, you don't get the 28 Hz frequency component showing up, even though it should have.

A 3 Hz signal shows up (an alias of 28 Hz), so the result is incorrect.
The result is surely correct! The 28Hz signal is aliased back to 3Hz. If you know what you are dealing with, this is not a problem. As I mentioned in one of my previous posts, sampling a signal at f=28Hz with 31Hz is a process called sub-sampling or undersampling (https://en.wikipedia.org/wiki/Undersampling). You can do it as long as the band of your signal is lower than fs/2. The max frequency could still be much larger than fs (there is a difference between bandwidth and frequency).
Communications_Engineer said:
Its like this, you are observing the following alias,
31 - 1 * 28 = 3 Hz
or generally,
F_alias = f ± k.Fs
where
f = frequency of desired (continuous-time signal that you want to sample)
k = 1 to inf
Fs = sampling frequency
Correct!
Communications_Engineer said:
There would be other aliases too, its just that you can't observe them since the frequency axis is limited to 0-Fs
Incorrect! For a pure sinewave there won't be any more aliases. Just two tones ona at -f the other one at +f.
 

I didn't know you have 20 years of experience. So have you used sub-sampling (pure, not multi-rate) with your algorithms?


Your proposition that sub-sampling can be used, if correct, would be very beneficial. But if the signal is a composite one (more than a single frequency), would it still work?

Kindly give a reference (except wikipedia). I wonder why most books don't talk about it.

Furthermore, I need a reference for the third part, i.e. a CT-sine wave, once sampled would not have any more aliases except fs-f and fs+f and no more aliases. Is the math wrong?
 

Communications_Engineer said:
So have you used sub-sampling (pure, not multi-rate) with your algorithms?
Undersampling is a very common.
Communications_Engineer said:
if the signal is a composite one (more than a single frequency), would it still work?
as long as the composite signal frequency bandwidth is less than fs/2, it always works. It is the Nyquist theorem applied to a band that is not baseband it is not black magic!
Communications_Engineer said:
Kindly give a reference (except wikipedia). I wonder why most books don't talk about it.
search the internet for "IF sampling", sub-sampling, undersampling, "direct IF receivers" and so on...
http://ipnpr.jpl.nasa.gov/progress_report/42-78/78L.PDF
**broken link removed**
Communications_Engineer said:
Furthermore, I need a reference for the third part, i.e. a CT-sine wave, once sampled would not have any more aliases except fs-f and fs+f and no more aliases. Is the math wrong?
This is basic Fourier transform: a pure sinewave in the frequency domain is represented by two delta functions, one at +f and the other at -f. In matlab, you need to use the function "fftshift" after fft in order to see the spectrum represented correctly (X11=fftshift(X1)). Your frequency axis should be defined as: f = k*Fs/length(X1)-Fs/2; then you can plot(f,abs(X11)) and see the two delta functions at +3 and -3.

I hope this help!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top