# 离散傅里叶变换

MATLAB® 环境提供 `fft``ifft` 函数，分别用于计算离散傅里叶变换及其逆变换。对于输入序列 x 及其变换版本 X（围绕单位圆的等间距频率的离散时间傅里叶变换），这两个函数实现以下关系

`$X\left(k+1\right)=\sum _{n=0}^{N-1}x\left(n+1\right){W}_{N}^{kn}$`

`$x\left(n+1\right)=\frac{1}{N}\sum _{k=0}^{N-1}X\left(k+1\right){W}_{N}^{-kn}.$`

`${W}_{N}={e}^{-j2\pi /N}.$`

```t = 0:1/100:10-1/100; % Time vector x = sin(2*pi*15*t) + sin(2*pi*40*t); % Signal```

```y = fft(x); % Compute DFT of x m = abs(y); % Magnitude y(m<1e-6) = 0; p = unwrap(angle(y)); % Phase```

```f = (0:length(y)-1)*100/length(y); % Frequency vector subplot(2,1,1) plot(f,m) title('Magnitude') ax = gca; ax.XTick = [15 40 60 85]; subplot(2,1,2) plot(f,p*180/pi) title('Phase') ax = gca; ax.XTick = [15 40 60 85];```

`fft` 的第二个参量指定变换的点数 `n`，表示 DFT 的长度：

```n = 512; y = fft(x,n); m = abs(y); p = unwrap(angle(y)); f = (0:length(y)-1)*100/length(y); subplot(2,1,1) plot(f,m) title('Magnitude') ax = gca; ax.XTick = [15 40 60 85]; subplot(2,1,2) plot(f,p*180/pi) title('Phase') ax = gca; ax.XTick = [15 40 60 85];```

```t = 0:1/255:1; x = sin(2*pi*120*t); y = real(ifft(fft(x))); figure plot(t,x-y)```