Fourier Transform by using MATLAB

11 次查看(过去 30 天)
Hello,
I want to use fft to get the frequency information of the signal in time domain. However, I find the frequency of the signal is determined by the sampling frequency. For example, if the sampling frequency is 1000 Hz, the signal frequency is in 100Hz, but when it changes to 2000Hz, it will be in 200Hz. Does anyone know how to solve this problem? The code is shown as following.
Thank you for your help.

采纳的回答

Jon
Jon 2019-6-26
Hi, I either can't reproduce your problem or am not understanding it.
Below I have inserted some sample code (which I think essentialy copies yours).
Since I didn't have your B3_noise signal I made one myself by creating a rand signal and then adding a single frequency component to it with a frequency of 100 Hz.
I ran it for two cases first with Fs = 1000 Hz and then with Fs = 2000 Hz. In both cases there is a clear spike, as expected at 100 Hz.
You will note that the maximum frequency on the two plots is different, 500 Hz in the first case and 1000 Hz in the second. This is just the Nyquist frequency (1/2 the sampling rate), which is the highest frequency that can be seen without aliasing in the FFT.
In the future, it is better to use the code button on the Matlab answers toolbar to insert code rather than using a screen shot. When you do it that way people can directly edit and try out your code snippet.
% assign the processing parameters
Fs = 2000; % sampling frequency
% assign the signal parameters
numPoints = 2500; % arbitrary length
fPure = 100; % frequency of single frequency component
aPure = 10; % amplitude of single frequency component
% calculate the sample period
T = 1/Fs;
% make the noise signal
t = (0:numPoints-1)*T;
B3_noise = rand(numPoints,1) + aPure*sin(2*pi*fPure*t(:)); % use t(:) to make sure it is a column
% emulate OP's code
% find signal length
L = length(B3_noise);
% calculate a time vector (although it isn't used for anything)
t = (0:L-1)*T;
% calculate FFT length as nearest power of 2
N = 2^nextpow2(L);
% make a vector of frequencies for plotting results
f = (Fs*(0:(N/2))/N)';
% calculate the N point FFT
Y = fft(B3_noise,N);
% calculate the single sided FFT magnitude
Y_mag = abs(Y/N);
Y_mag = Y_mag(1:N/2+1)/(N/2);
% plot the results
figure
plot(f,Y_mag)
title(['Fs = ',num2str(Fs)])
Fs1000.jpg
Fs2000.jpg
  1 个评论
Jon
Jon 2019-6-26
Hi Di,
I couldn't download your .mat file, but that is OK. I think I now understand your problem. It is a conceptual problem in applying the FFT tool, not a problem with your MATLAB code.
If you have actual data that was obtained from a measurement system at a particular sampling frequency, then when you use MATLAB to do the FFT you must tell it (specify Fs) as the actual sampling frequency that was used when the data was taken. So if the data was actually obtained by sampling at 1000 Hz by the measurement device, then you must specify Fs = 1000 in your code.
The underlying issue here is that the measurements, in your case B3_noise.mat, are just a vector of values. There is no information there about how far apart they are in time. You tell MATLAB how far apart they are in time, T, or equivalently its reciprocal Fs in your code. So if you really took the data at 1000 Hz and then tell MATLAB that Fs is 2000 Hz you are implying that your data points are spaced at 1/2000 seconds intervals when really they are spaced at 1/1000 second intervals. So a signal which is really at 100 hz looks like it is going at 200 hz.
Summary - you must specify the sampling frequency to match what was actually used when the data was taken.
p.s. I also put this as a comment on your "Answer" below, but put it here also, as this is the accepted answer, and so it is more likely this is where people will look to find out what the solution to your problem was.

请先登录,再进行评论。

更多回答(1 个)

di liu
di liu 2019-6-26
Thank you Jonathan,
It is my first time to ask question in MATLAB Community, so I donot know how to add the code. I am sorry for the inconvenience caused by me. I try this code in my PC, but it still does work. I have uploaded the B3_noise as an attachment, could you try this in your PC? Thank you.
% Using FFT to obtain the frequency spectrum
Fs=1000; % Sampling Frequency
T=1/Fs;
L=length(B3_noise);
t=(0:L-1)*T;
N=2^nextpow2(L);
f=(Fs*(0:(N/2))/N)';
Y=fft(B3_noise,N);
Y_mag=abs(Y/N);
Y_mag=Y_mag(1:N/2+1)/(N/2);
% B(2:end-1)=2*B(2:end-1);
figure (1)
plot(f,Y_mag);
ylim([0 0.1]);
figure (2)
plot(B3_noise);
  3 个评论
Jon
Jon 2019-6-26
Hi Di,
I couldn't download your .mat file, but that is OK. I think I now understand your problem. It is a conceptual problem in applying the FFT tool, not a problem with your MATLAB code.
If you have actual data that was obtained from a measurement system at a particular sampling frequency, then when you use MATLAB to do the FFT you must tell it (specify Fs) as the actual sampling frequency that was used when the data was taken. So if the data was actually obtained by sampling at 1000 Hz by the measurement device, then you must specify Fs = 1000 in your code.
The underlying issue here is that the measurements, in your case B3_noise.mat, are just a vector of values. There is no information there about how far apart they are in time. You tell MATLAB how far apart they are in time, T, or equivalently its reciprocal Fs in your code. So if you really took the data at 1000 Hz and then tell MATLAB that Fs is 2000 Hz you are implying that your data points are spaced at 1/2000 seconds intervals when really they are spaced at 1/1000 second intervals. So a signal which is really at 100 hz looks like it is going at 200 hz.
Summary - you must specify the sampling frequency to match what was actually used when the data was taken.
di liu
di liu 2019-6-27
Hello,Jonathan,
I really like your answer, it helps me a lot. Thank you for your warm answer. Have a nice day.
Di

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by