22 views (last 30 days)

I have this example and I'm puzzled with the frequency vector (hz) if I change any parameter in it, the result won't be the frequency components. How to calculate the frequency vector, generally?

srate = 1000;

time = -2: 1/srate : 2;

pnts = length(time);

hz = linspace(0,srate/2,(pnts-1)/2);

freq = [2 10 25 40];

amp = [3 5 7 9];

phas = [-pi/4 pi/2 pi 2*pi];

signal = zeros(size(time));

for i = 1:length(freq)

signal = signal + amp(i)*sin( 2*pi*freq(i)*time + phas(i));

end

subplot(211)

plot(time,signal,'k-','linew',0.5)

title("Time Domain")

y = fft(signal)/length(signal);

pwr = abs( y ).^2;

pwr(1) = [];

subplot(212)

plot(hz,pwr(1:length(hz)),'r-')

title("Frequency Domain")

Cris LaPierre
on 24 Dec 2020

Edited: Cris LaPierre
on 24 Dec 2020

I think I would explain it this way. Due to Nyquist theory, the highest frequency you can detect is half your sample rate (must have at least 2 points in one period). The lowest is obviously 0. The assumption here is that the detectable frequencies increase linearly from 0 to srate/2.

If you inspect the peaks in your plot, you'll notice this approach is an approximation, as the peaks are about 1 point off.

Cris LaPierre
on 25 Dec 2020

Not without having to change some other things. The number of points is set by your sample rate. Changing it directly impacts the results, as you've observed. To get the frequencies to line up, you'd have to half your sample rate, which halves the frequencies you can resolve in the signal.

Better to zoom in on your axis if you want to focus on the peaks. Use xlim.

srate = 1000;

time = -2: 1/srate : 2;

pnts = length(time);

hz = linspace(0,srate/2,(pnts-1)/2);

freq = [2 10 25 40];

amp = [3 5 7 9];

phas = [-pi/4 pi/2 pi 2*pi];

signal = zeros(size(time));

for i = 1:length(freq)

signal = signal + amp(i)*sin( 2*pi*freq(i)*time + phas(i));

end

y = fft(signal)/length(signal);

pwr = abs( y ).^2;

pwr(1) = [];

plot(hz,pwr(1:length(hz)),'r-')

title("Frequency Domain")

xlim([0 250])

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226632

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226632

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226792

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226792

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226832

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226832

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226892

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/701942-how-to-make-the-frequency-vector-to-analyze-a-complex-signal#comment_1226892

Sign in to comment.