Can i plot spectrum of a signal in Matlab

5 views (last 30 days)
I want to plot spectrum diagram which gives the frequency content of a signal x(t) for example if i draw spectrum of x(t) = 14 cos(200*pi*t -pi/3)
by hand it will be two arrows at -100 and +100 with values 7e^(jpi/3) and 7e^(-jpi/3)
Can some software or matlab can do this for me

Accepted Answer

Wayne King
Wayne King on 1 Oct 2011
Fs = 1000;
t = 0:1/Fs:1-(1/Fs);
x = 14*cos(200*pi*t-pi/3);
xdft = (1/length(x))*fft(x);
freq = -500:(Fs/length(x)):500-(Fs/length(x));
plot(freq,abs(fftshift(xdft)));
  2 Comments
Wayne King
Wayne King on 1 Oct 2011
Your frequency above is actually 100 Hz (2*100*pi*t). You have not given the sampling rate, so I just assumed 1000 Hz here.
Amna
Amna on 12 Oct 2023
Can anyone plz explain what will be the spectrum of j*cos(2*pi*10*t) , j*exp(-j*2*pi*10*t),
-j*exp(-j*2*pi*10*t) and -j*cos(2*pi*10*t) ?? please plot them i'm not asking for magnitude plot show me actual plots please help.

Sign in to comment.

More Answers (6)

moonman
moonman on 1 Oct 2011
Wonderful Wayne King. u again rocked
Can u tell me what will be the minimum sampling rate that will be required to sample the signal without aliasing. Since the frequency is 100 Hz, so i think 200 Hz is minimum. am i right
Secondly i want to ask that if the signal is like this cos(1000*pi*t) +sin(300*pi*t) so we have to see the largest frequency component in the equation and according to that we will select minimum freq to avoid aliasing. So in this example it will be 1000 Hz (500*2 as per nyqust).* Am i right*
  2 Comments
Wayne King
Wayne King on 1 Oct 2011
Yes, 200 is right, but I would sample at least a bit more than that, 200 would put the 100 Hz component right at the Nyquist. I would avoid that.
Not sure exactly what you're asking in the 2nd, you should sample based on the bandwidth of your signal, which is 2000 Hz here (you have a component at -1000 and 1000) so 1000-(-1000)=2000. You have to sample at least at the bandwidth (2 kHz).
Wayne King
Wayne King on 1 Oct 2011
sorry I misread above-- your bandwidth is 1 kHz (500-(-500))=1000. I misread the frequency as (2*1000*pi) instead of (1000*pi). you have to sample at least at the bandwidth. (1 kHz).

Sign in to comment.


moonman
moonman on 1 Oct 2011
In this signal
x(t) = 14 cos(200*pi*t -pi/3)
we have to write it like
x(t) = 14 cos(2*pi*100*t -pi/3) ----> cos(2*pi*f*t) form
So freq is 100 Hz ---*Am i right*
Similarly in equation
cos(1000*pi*t) +sin(300*pi*t)
the max frequency is 500 Hz and other is 150 Hz.
So minimum sampling rate for this signal as per nyquist will be 500*2=1000Hz
am i right
  1 Comment
Wayne King
Wayne King on 1 Oct 2011
Oh sorry! yes, for some reason, I read 2*pi*1000 in your first post, you are right. it's 500 Hz so it has to be sampled at a minimum of 1 kHz.

Sign in to comment.


moonman
moonman on 1 Oct 2011
One last query regarding plot
It is nice to see the spectrum through code but it does not show the amplitude values i mean e^(j*pi) what so ever they are how can i come to know abt those or i have to do calcualtions by hand for those
  2 Comments
moonman
moonman on 1 Oct 2011
Furthermore code is not working when i have to multiply two waveform
x = cos(1000*pi*t+pi/3)*(sin(500*pi*t+pi/4))
Code is giving error
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> spectrum at 3
x = cos(1000*pi*t+pi/3)*(sin(500*pi*t+pi/4));
Wayne King
Wayne King on 1 Oct 2011
The factor your refer to is the phase, you can get that by plotting the angle() (but that tends to be messy unless you unwrap it which is another matter), or you can query it directly. For example, xdft(101) corresponds to the DFT bin for 100 Hz in the example I gave you. Because the bins are spaced at Fs/length(x) with the first bin corresponding to zero frequency, DC. If you query
angle(xdft(101))
you get -1.0472, which is -pi/3

Sign in to comment.


Wayne King
Wayne King on 1 Oct 2011
as far as your multiplication problem, use the .* operator
x = cos(1000*pi*t+pi/3).*(sin(500*pi*t+pi/4));
  2 Comments
moonman
moonman on 1 Oct 2011
ok i m also getting error with dot operator
Fs = 1000;
t = 0:1/Fs:1-(1/Fs);
x = cos(1000*pi*t+pi/3).*(sin(500*pi*t+pi/4));
xdft = (1/length(x))*fft(x);
freq = -2000:(Fs/length(x)):2000-(Fs/length(x));
plot(freq,abs(fftshift(xdft)))
moonman
moonman on 1 Oct 2011
Sorry Problem is resolved
I was not putting one bracket

Sign in to comment.


Wayne King
Wayne King on 1 Oct 2011
That is because you changed the sampling frequency, in your frequency vector
freq = -2000:(Fs/length(x)):2000-(Fs/length(x));
Your frequency vector is set up for a 4000 Hz sampling rate. However you left
Fs = 1000;
So your sizes are not correct.
Also you are creating a modulated signal above, so you have to think about what the spectrum of that signal is. That is different than just adding components.
Fs = 4000;
t = 0:1/Fs:1-(1/Fs);
x = cos(1000*pi*t+pi/3).*(sin(500*pi*t+pi/4));
xdft = (1/length(x))*fft(x);
freq = -2000:(Fs/length(x)):2000-(Fs/length(x));
plot(freq,abs(fftshift(xdft)));

moonman
moonman on 1 Oct 2011
Great Answer Bundles of Thanks again

Tags

Community Treasure Hunt

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

Start Hunting!