Matlab perform fft to obtain amplitude and phase

In this article, we imposed Matlab fft on a periodic function and to get its amplitude and phase

code

sampleFreq=100;
x=[-100:1/sampleFreq:100-1/sampleFreq];
nLength=length(x);%20000
fftLength=floor(nLength/2);
frequencyVector=(0:1/nLength*sampleFreq:(nLength-1)/nLength*sampleFreq);
y1=1.7*cos(10*2*pi*x+pi/4);
result=fft(y1,nLength);
result1=result(1:fftLength);
phase=atan2(imag(result1),real(result1));
result=2*abs(result)/nLength;
%plot(y1);
figure;
plot(frequencyVector(1:fftLength),result(1:fftLength));
xlabel("frequency")
ylabel("amplitude")
grid on;
%hold on;
%plot(frequencyVector(1:fftLength),phase(1:fftLength));
maxAmpli=max(result)
index=find(result==maxAptil)
thisPhase=phase(index(1))
freq=frequencyVector(index(1))

result

result 1:
在这里插入图片描述
result 2:

maxAmpli =
    1.7000
index =
        2001       18001
thisPhase =
    0.7854
freq =
    10

Note:

  1. If the function is relating to sin(), the correct phase should plus pi/2 due to the fft function is powered by cos() in matlab.
  2. To get right phase, the number of sample points should be even due to unknown error.
Sure, here is an example code to calculate the magnitude and phase of an input frame using FFT and IFFT in C language: ```c #include <stdio.h> #include <math.h> #include <complex.h> #include <fftw3.h> #define N 256 // Input frame size int main() { double input[N]; // Input frame double output[N]; // Output frame double magnitude[N/2+1]; // Magnitude spectrum double phase[N/2+1]; // Phase spectrum // Initialize input frame (for example, with a sine wave) for (int i = 0; i < N; i++) { input[i] = sin(2*M_PI*i/N); } // Perform FFT fftw_complex fft_output[N/2+1]; fftw_plan fft_plan = fftw_plan_dft_r2c_1d(N, input, fft_output, FFTW_ESTIMATE); fftw_execute(fft_plan); fftw_destroy_plan(fft_plan); // Calculate magnitude and phase for (int i = 0; i < N/2+1; i++) { magnitude[i] = cabs(fft_output[i]); phase[i] = carg(fft_output[i]); } // Perform IFFT fftw_plan ifft_plan = fftw_plan_dft_c2r_1d(N, fft_output, output, FFTW_ESTIMATE); fftw_execute(ifft_plan); fftw_destroy_plan(ifft_plan); // Normalize output frame for (int i = 0; i < N; i++) { output[i] /= N; } // Print results for (int i = 0; i < N/2+1; i++) { printf("Bin %d: Magnitude = %f, Phase = %f\n", i, magnitude[i], phase[i]); } return 0; } ``` In this code, we first initialize an input frame of size N (in this case, a sine wave). We then perform FFT using the `fftw_plan_dft_r2c_1d` function from the FFTW library. This function takes the input frame, the output buffer (in this case, a complex array `fft_output`), and a flag indicating the FFT algorithm to use (`FFTW_ESTIMATE` in this case, which is a good default choice). We then calculate the magnitude and phase of each frequency bin using the `cabs` and `carg` functions from the C standard library, respectively. Finally, we perform IFFT using the `fftw_plan_dft_c2r_1d` function, and normalize the output frame by dividing by N. The results are printed to the console. Note that this code is just an example, and may need to be modified depending on the specific requirements of your application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值