Decentralization of MATLAB FFT
Publish: 2021-04-15 23:47:46
1.
x = load('data.dat'); %load 数据
fs=10000; % 采样频率,自己根据实际情况设置
N=length(x); % x 是待分析的数据
n=1:N;
%1-FFT
X=fft(x); % FFT
X=X(1:N/2);
Xabs=abs(X);
Xabs(1) = 0; %直流分量置0
for i= 1 : m
[Amax,index]=max(Xabs);
if(Xabs(index-1) > Xabs(index+1))
a1 = Xabs(index-1) / Xabs(index);
r1 = 1/(1+a1);
k01 = index -1;
else
a1 = Xabs(index) / Xabs(index+1);
r1 = 1/(1+a1);
k01 = index;
end
Fn = (k01+r1-1)*fs/N; %基波频率
An = 2*pi*r1*Xabs(k01)/(N*sin(r1*pi)); %基波幅值
Pn = phase(X(k01))-pi*r1; %基波相角 单位弧度
Pn = mod(Pn(1),pi);
end
2. Matlab FFT abscissa problem: predecessors on FFT abscissa in detail
we know that Fourier analysis is a very important technology in signal processing, matlab provides a strong signal processing ability, but there are some details we need to pay attention to
the starting time of signal f (T) is t_ Start, the end time is t_ End, the sampling period is t_ s. The ration of the signal can be calculated as t_ end – t_ Start, sampling points caused by signal discretization n = ration / T_ s + 1;
according to the relevant conclusions of Fourier analysis, we know that sampling in time domain will cause periodization in frequency domain, which is sampling frequency f_ S (the famous Shannon sampling theorem is based on this).
therefore, after the FFT function processing of MATLAB, the abscissa of the data is 0: F_ s/(N-1):f_ s The related codes are as follows:
% matlab FFT test code
t_ s = 0.01;< br /> t_ start = 0.5; t_ end = 5;< br /> t = t_ start:t_ s:t_ end;< br /> y = 0.5*sin(2*pi*15*t)+2*sin(2*pi*40*t);< br /> y_ f = fft(y);< br />subplot(3,1,1);< br /> plot(t,y); title(' original signal');< br />Duration = t_ end - t_ start;< br /> Sampling_ points = Duration/t_ s + 1;< br />f_ s = 1/t_ s;< br /> f_ x = 0:f_ s/(Sampling_ points-1):f_ s;< br />subplot(3,1,2);< br /> plot(f_ x,abs(y_ f)); title(' fft transform');< br />subplot(3,1,3);< br /> plot(f_ x-f_ s/2,abs(fftshift(y_ f))); title(' shift fft transform');
in other words, if we don't use fftshift, the abscissa after transformation is 0: F_ s/(N-1):f_ s. If you use the fftshift command, the 0 frequency component will move to the coordinate center, which is the meaning given by the help center in Matlab: the coordinate of FFT is processed. In fact, e to the periodicity of the spectrum, it is reasonable and acceptable for us to do so.
we know that Fourier analysis is a very important technology in signal processing, matlab provides a strong signal processing ability, but there are some details we need to pay attention to
the starting time of signal f (T) is t_ Start, the end time is t_ End, the sampling period is t_ s. The ration of the signal can be calculated as t_ end – t_ Start, sampling points caused by signal discretization n = ration / T_ s + 1;
according to the relevant conclusions of Fourier analysis, we know that sampling in time domain will cause periodization in frequency domain, which is sampling frequency f_ S (the famous Shannon sampling theorem is based on this).
therefore, after the FFT function processing of MATLAB, the abscissa of the data is 0: F_ s/(N-1):f_ s The related codes are as follows:
% matlab FFT test code
t_ s = 0.01;< br /> t_ start = 0.5; t_ end = 5;< br /> t = t_ start:t_ s:t_ end;< br /> y = 0.5*sin(2*pi*15*t)+2*sin(2*pi*40*t);< br /> y_ f = fft(y);< br />subplot(3,1,1);< br /> plot(t,y); title(' original signal');< br />Duration = t_ end - t_ start;< br /> Sampling_ points = Duration/t_ s + 1;< br />f_ s = 1/t_ s;< br /> f_ x = 0:f_ s/(Sampling_ points-1):f_ s;< br />subplot(3,1,2);< br /> plot(f_ x,abs(y_ f)); title(' fft transform');< br />subplot(3,1,3);< br /> plot(f_ x-f_ s/2,abs(fftshift(y_ f))); title(' shift fft transform');
in other words, if we don't use fftshift, the abscissa after transformation is 0: F_ s/(N-1):f_ s. If you use the fftshift command, the 0 frequency component will move to the coordinate center, which is the meaning given by the help center in Matlab: the coordinate of FFT is processed. In fact, e to the periodicity of the spectrum, it is reasonable and acceptable for us to do so.
3. First of all, FFT is used to analyze the spectrum of the experimental data to find the frequency range of the signal and noise you need. Then, all the noise bands are set to zero. Finally, the denoised signal is processed by IFFT to restore the signal.
4. s=importdata(' s.txt');< br />y=fft(s,1024); % 1024 is introced because the FFT of integer power point of 2 is better. Calling this function is equivalent to filling zero of 1000 s to 1024 points and doing 1024 points FFT. The effect is equivalent to increasing sampling points in frequency domain
PYY = y. * conj (y);% Not equivalent to PPy = ABS (y), equivalent to PPy = ABS (y). ^ 2
F = 10000 * (0:512) / 1024;% Because the abscissa is in 1kHz, the frequency should be 10000khz, so 10000 represents this meaning. The middle point of FFT (512 points starting with 0 or 513 points starting with 1) represents the frequency component of half sampling frequency.
plot (F, PYY (1:513), - 39;); % Plot (f (1:25), PYY (1:25), & 39;, if you want to reach the frequency range of the left figure- 39;), However, we can see that the number of points drawn is too small.
therefore, I suggest using the following sentence
s = importdata (& 39; s.txt');< br />y=fft(s,10000);< br />pyy=y.*conj(y);< br />f=10000*(0:250)/10000;< br />plot(f(1:251),pyy(1:251),'.- 39;);
because there is no original data of S, I can't draw a picture for you. I guess it is a sinusoidal signal with Hanning window
PYY = y. * conj (y);% Not equivalent to PPy = ABS (y), equivalent to PPy = ABS (y). ^ 2
F = 10000 * (0:512) / 1024;% Because the abscissa is in 1kHz, the frequency should be 10000khz, so 10000 represents this meaning. The middle point of FFT (512 points starting with 0 or 513 points starting with 1) represents the frequency component of half sampling frequency.
plot (F, PYY (1:513), - 39;); % Plot (f (1:25), PYY (1:25), & 39;, if you want to reach the frequency range of the left figure- 39;), However, we can see that the number of points drawn is too small.
therefore, I suggest using the following sentence
s = importdata (& 39; s.txt');< br />y=fft(s,10000);< br />pyy=y.*conj(y);< br />f=10000*(0:250)/10000;< br />plot(f(1:251),pyy(1:251),'.- 39;);
because there is no original data of S, I can't draw a picture for you. I guess it is a sinusoidal signal with Hanning window
5. 1. The sampling data is imported into Matlab
there are at least three ways to import sampling data
the first is to manually organize the data into the format supported by MATLAB, which is only suitable for sampling with small amount of data
the second method is to use the visual interactive operation of MATLAB, and the specific operation steps are: File -- & gt; Import data, and then find the file to save the sampling data in the pop-up dialog box, and import the data step by step according to the prompts. This method is suitable for large amount of data, but not too large data
the third method is to use the file read in command. The data file reading commands include textread, fscanf, load, etc. if the sampling data is saved in the txt file, the textread command is recommended. Such as [a, b] = texture (# 39; data.txt', 39;% f%*f%f'); This command divides the data saved in data.txt into three groups, the first data of each group is sent to column vector a, the third data is sent to column vector B, and the second data is discarded. Command is similar to C language, you can see its help file for details. File read command input sampling data can handle any size of data, and the input speed is quite fast, more than 1 million data can be input in less than 20 seconds
2
it is natural to use FFT in spectrum analysis. The corresponding command is FFT. The simple method is: y = FFT (B, n), where B is the sampling data and N is the number of FFT data samples. Generally, n is not specified, that is, y = FFT (b). Y is the result of FFT transformation, which is equal to the number of elements of B and is a complex number. Taking the frequency as the abscissa and the amplitude of each element of Y array as the ordinate, the amplitude frequency characteristics of data B can be obtained by drawing; Taking frequency as abscissa and the angle of each element of Y array as ordinate, the phase frequency characteristics of data B can be obtained by drawing. Examples of typical spectrum analysis m program are as follows: CLC FS = 100< br />t=[0:1/fs:100];< br />N=length(t)-1;% Subtract 1 to make n even% frequency resolution f = 1 / T = FS / N
P = 1.3 * sin (0.48 * 2 * pi * t) + 2.1 * sin (0.52 * 2 * pi * t) + 1.1 * sin (0.53 * 2 * pi * t)... + 0.5 * sin (1.8 * 2 * pi * t) + 0.9 * sin (2.2 * 2 * pi * t)
% the upper part samples the signal to get the sampling data p, and then analyzes the spectrum of P
figure (1) plot (T, P); grid on
title(' Signal p (T) & (39;); xlabel(' t') ylabel(' p') Y=fft(p);< br />magY=abs(Y(1:1:N/2))*2/N; f=(0:N/2-1)'* fs/N; figure(2)
%plot(f,magY);< br />h=stem(f,magY,' fill', 39;-- 39;);< br />set(h,' MarkerEdgeColor', 39; red', 39; Marker', 39;* 39;) grid on
title(' Spectrum (ideal value: [0.48hz, 1.3], [0.52hz, 2.1], [0.53hz, 1.1], [1.8hz, 0.5], [2.2Hz, 0.9]) and #; xlabel(' f (Hz)') ylabel(' Amplitude & (39;)
in reality, the sampling frequency FS is generally determined by the sampling instrument, that is, FS is a given constant; On the other hand, in order to obtain a certain precision spectrum, there is a man-made regulation for frequency resolution F, which generally requires F & lt; 01, that is, sampling time Ts & gt; 100 seconds; The amount of sampling data can be determined by the sampling time Ts and sampling frequency FS, that is, the total number of sampling points n = FS * ts. This requires the sampling time Ts and the total number of sampling points n in theory to ensure the accuracy of spectrum analysis.
there are at least three ways to import sampling data
the first is to manually organize the data into the format supported by MATLAB, which is only suitable for sampling with small amount of data
the second method is to use the visual interactive operation of MATLAB, and the specific operation steps are: File -- & gt; Import data, and then find the file to save the sampling data in the pop-up dialog box, and import the data step by step according to the prompts. This method is suitable for large amount of data, but not too large data
the third method is to use the file read in command. The data file reading commands include textread, fscanf, load, etc. if the sampling data is saved in the txt file, the textread command is recommended. Such as [a, b] = texture (# 39; data.txt', 39;% f%*f%f'); This command divides the data saved in data.txt into three groups, the first data of each group is sent to column vector a, the third data is sent to column vector B, and the second data is discarded. Command is similar to C language, you can see its help file for details. File read command input sampling data can handle any size of data, and the input speed is quite fast, more than 1 million data can be input in less than 20 seconds
2
it is natural to use FFT in spectrum analysis. The corresponding command is FFT. The simple method is: y = FFT (B, n), where B is the sampling data and N is the number of FFT data samples. Generally, n is not specified, that is, y = FFT (b). Y is the result of FFT transformation, which is equal to the number of elements of B and is a complex number. Taking the frequency as the abscissa and the amplitude of each element of Y array as the ordinate, the amplitude frequency characteristics of data B can be obtained by drawing; Taking frequency as abscissa and the angle of each element of Y array as ordinate, the phase frequency characteristics of data B can be obtained by drawing. Examples of typical spectrum analysis m program are as follows: CLC FS = 100< br />t=[0:1/fs:100];< br />N=length(t)-1;% Subtract 1 to make n even% frequency resolution f = 1 / T = FS / N
P = 1.3 * sin (0.48 * 2 * pi * t) + 2.1 * sin (0.52 * 2 * pi * t) + 1.1 * sin (0.53 * 2 * pi * t)... + 0.5 * sin (1.8 * 2 * pi * t) + 0.9 * sin (2.2 * 2 * pi * t)
% the upper part samples the signal to get the sampling data p, and then analyzes the spectrum of P
figure (1) plot (T, P); grid on
title(' Signal p (T) & (39;); xlabel(' t') ylabel(' p') Y=fft(p);< br />magY=abs(Y(1:1:N/2))*2/N; f=(0:N/2-1)'* fs/N; figure(2)
%plot(f,magY);< br />h=stem(f,magY,' fill', 39;-- 39;);< br />set(h,' MarkerEdgeColor', 39; red', 39; Marker', 39;* 39;) grid on
title(' Spectrum (ideal value: [0.48hz, 1.3], [0.52hz, 2.1], [0.53hz, 1.1], [1.8hz, 0.5], [2.2Hz, 0.9]) and #; xlabel(' f (Hz)') ylabel(' Amplitude & (39;)
in reality, the sampling frequency FS is generally determined by the sampling instrument, that is, FS is a given constant; On the other hand, in order to obtain a certain precision spectrum, there is a man-made regulation for frequency resolution F, which generally requires F & lt; 01, that is, sampling time Ts & gt; 100 seconds; The amount of sampling data can be determined by the sampling time Ts and sampling frequency FS, that is, the total number of sampling points n = FS * ts. This requires the sampling time Ts and the total number of sampling points n in theory to ensure the accuracy of spectrum analysis.
6. Look at the following program, which should help you. It has passed the debugging:
FS = 256;% Sampling frequency (Hz)
n = 256;% Sampling points
t = [0:1 / FS: n / Fs];% Sampling time
s = 2 + 3 * cos (2 * pi * 10 * t + pi * 30 / 180) + cos (2 * pi * 20 * t + pi * 90 / 180)
% my debugging signal, if you are the current and voltage data, you can load it through the load command at the beginning, which is
y = FFT (s, n);% Do FFT
Ayy = ABS (y);% Molus
Ayy = Ayy / (n / 2);% The actual range is
Ayy (1) = Ayy (1) / 2< br />F=([1:N]-1)*Fs/N; % Converted to the actual frequency value, FN = (n-1) * FS / N
stem (f (1: n / 2), Ayy (1: n / 2));% Display the converted FFT molus results
title (&? 39; Amplitude frequency curve (# 39;);
FS = 256;% Sampling frequency (Hz)
n = 256;% Sampling points
t = [0:1 / FS: n / Fs];% Sampling time
s = 2 + 3 * cos (2 * pi * 10 * t + pi * 30 / 180) + cos (2 * pi * 20 * t + pi * 90 / 180)
% my debugging signal, if you are the current and voltage data, you can load it through the load command at the beginning, which is
y = FFT (s, n);% Do FFT
Ayy = ABS (y);% Molus
Ayy = Ayy / (n / 2);% The actual range is
Ayy (1) = Ayy (1) / 2< br />F=([1:N]-1)*Fs/N; % Converted to the actual frequency value, FN = (n-1) * FS / N
stem (f (1: n / 2), Ayy (1: n / 2));% Display the converted FFT molus results
title (&? 39; Amplitude frequency curve (# 39;);
7.
1. Using Ceil Function directly, you can round up: that is, the smallest integer greater than or equal to the given data

8. 1. Generally, the sampling points in frequency domain are larger than those in time domain, and it is better to be idempotent of 2, which is convenient for calculation. Let's take a look at books like digital signal processing. 2. Suppose the sampling frequency is FS, the signal frequency is f, and the number of sampling points is n. Then the result after FFT is a complex number of n points. Each point corresponds to a frequency point. The mode value of this point is the amplitude characteristic at this frequency value. What does it have to do with the amplitude of the original signal? Suppose the peak value of the original signal is a, then the molus value of each point (except the DC component of the first point) of the FFT result is n / 2 times of a, so it should be 3 linspace (x0, x1, n), where n represents the number of points, that is, divided into n-1 equal parts. In fact, FS / 2 * linspace (0,1, nfft / 2 + 1); It is divided into nfft / 2 between 0 and 1, that is, FS / nfft, that is, the frequency of setting the interval point. Finally, 2 * ABS (Y (1: nfft / 2 + 1)) is multiplied by 2 because the front y = FFT (x, nfft) / nfft is half of the original signal
suppose that a point n after FFT is represented by complex a + bi, then the molus of the complex is
an = radical a * a + b * B, and the phase is PN = atan2 (B, a). According to the above results,
can calculate n points (n ≠ 1, and N & lt= The expression of the corresponding signal is:
an / (n / 2) * cos (2 * pi * FN * t + PN), that is, 2 * an / N * cos (2 * pi * FN * t + PN)
for the signal with n = 1 point, it is DC component, and the amplitude is A1 / n< Because of the symmetry of FFT results, we usually only use the results of the first half,
that is, the results less than half of the sampling frequency.
suppose that a point n after FFT is represented by complex a + bi, then the molus of the complex is
an = radical a * a + b * B, and the phase is PN = atan2 (B, a). According to the above results,
can calculate n points (n ≠ 1, and N & lt= The expression of the corresponding signal is:
an / (n / 2) * cos (2 * pi * FN * t + PN), that is, 2 * an / N * cos (2 * pi * FN * t + PN)
for the signal with n = 1 point, it is DC component, and the amplitude is A1 / n< Because of the symmetry of FFT results, we usually only use the results of the first half,
that is, the results less than half of the sampling frequency.
9. FFT spectrum itself is symmetrical, generally take the first half of the spectrum.
Hot content
