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.

how to plot the signal in frequency domain by MATLAB?

Status
Not open for further replies.

triquent

Full Member level 3
Joined
Oct 13, 2004
Messages
166
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
1,826
frequency domain plot

For signal y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t), f1=8Hz, f2=32hz, f3=128hz, how to plot the signal in frequency domain using MATLAB? I know how to plot it in time domain? but for frequency domain, should I just plot the 6 one samples at the corresponding frequency?
 

plot frequency domain matlab

Take a look into the FFTcomand in Matlab help. It computes the Fast Fourier Transform of the signal and so gives the components in the frequency domain.
 

time domain to frequency domain in matlab

If you need more samples in the frequency domain, add the some zeros to the end of the signal before fft
 

matlab frequency domain plot

f1=8; f2=32;, f3=128;
t=0:.01:1; % Signal length in time
y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t);
figure; plot(y);
lfft=256; % FFT size
yf=fft(y,lfft);
figure;plot((0:lfft-1)/lfft,abs(yf));
 

plot frequency domain

if we need to generate 2048 samples of this signal, should I set lfft=2048?
Also is it possible to plot this signal in frequency domain using freqz()?

ducdep said:
f1=8; f2=32;, f3=128;
t=0:.01:1; % Signal length in time
y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t);
figure; plot(y);
lfft=256; % FFT size
yf=fft(y,lfft);
figure;plot((0:lfft-1)/lfft,abs(yf));
 

matlab plot frequency domain

hi
to have more samples u should increase ur time domain samples.
"ducdep" set sample rate = 100 and set lfft = 256 whereas lfft>sample rate .
istead try this:

a = 1/2048
t = 0:a:1;
y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t);
plot(1000*t(1:50),y(1:50))

Y = fft(y,512);
Pyy = Y.* conj(Y) / 512;
f = 1000*(0:256)/512;
plot(f,Pyy(1:257))
title('Frequency content of y')
xlabel('frequency (Hz)')

sample rate is set to a = 1/2048

about ur first question: it is not necessary to set lfft equal to ur no. of samples but if u do so
it has the best answer because in this manner matlab DOESN'T IGNORE some of the samples of ur
signal. and also try the PWELCH command to have an averaged autospectra.
2nd question i haven't tried it.
hope to be helpful
 

frequency domain plot matlab

Simple answer

use fvtool(x) command to see spectrum of signal x

That's all :)
 

time domain to frequency domain matlab

thank all of you for answering the questions. If I want to use the freqz() to draw the frequecy domain plot. I did this way: here f1=8Hz, f2=32Hz, f3=128Hz, fs=2048Hz
y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t)=1/2[exp(jw1)+exp(-jw1)+exp(jw2)+exp(-jw2)+exp(jw3)+exp(-jw3)]
so y[n]=1/2(x[n-1]+x[n-4]+x[n-16])+.... for exp(-jw) terms. Then how about the exp(jw) terms? after got the difference equation , we got the coefficients bb=[.....]
Then use freqz(bb,1,[-pi:(pi/100):pi])
Is this right? How to get the exp(jw) terms' difference equation?
 

matlab time domain frequency domain

Thanks. but Pyy = Y.* conj(Y) / 512; is the power spectrum, isn't it? should i use power spectrum to represent the signal in frequency domain?
[quote="mro8hi
to have more samples u should increase ur time domain samples.
"ducdep" set sample rate = 100 and set lfft = 256 whereas lfft>sample rate .
istead try this:

a = 1/2048
t = 0:a:1;
y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t);
plot(1000*t(1:50),y(1:50))

Y = fft(y,512);
Pyy = Y.* conj(Y) / 512;
f = 1000*(0:256)/512;
plot(f,Pyy(1:257))
title('Frequency content of y')
xlabel('frequency (Hz)')

sample rate is set to a = 1/2048

about ur first question: it is not necessary to set lfft equal to ur no. of samples but if u do so
it has the best answer because in this manner matlab DOESN'T IGNORE some of the samples of ur
signal. and also try the PWELCH command to have an averaged autospectra.
2nd question i haven't tried it.
hope to be helpful[/quote]

Added after 4 hours:

BTW, How to read the unit of magnitude plot. In the magnitude plot of abs(yf), yf=fft(y), the magnitude will be several hundred to thousand. But if you look at the three frequency here, all of their magnitude should be one. why when using the fft , the magnitude of the three cosine signals is so large? is there anyway to plot it correctly?
ducdep said:
f1=8; f2=32;, f3=128;
t=0:.01:1; % Signal length in time
y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t);
figure; plot(y);
lfft=256; % FFT size
yf=fft(y,lfft);
figure;plot((0:lfft-1)/lfft,abs(yf));
 

matlab time domain to frequency domain

you are right triquent!

plotting Pyy as autospectra is for illustration.

u can simply plot "Y" as fft of the signal. as follows :

a = 1/2048
t = 0:a:1;
f1=8; f2=32;, f3=128;
y=cos(2*pi*f1*t)+cos(2*pi*f2*t)+cos(2*pi*f3*t);
plot(1000*t(1:50),y(1:50))

Y = fft(y,512);
f = a*(0:256)/512;
figure; plot(f,Y(1:257))

and also in defining "f" as frequency 1000 should be changed to a (sampling frequency).
cheers
 

frequency domain plot in matlab

Hi

Try using "pwelch", which plots power spectral density directly

Regards
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top