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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值