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.
0 件のコメント
採用された回答
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)])
1 件のコメント
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
2019 年 6 月 26 日
3 件のコメント
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!