用matlab做:已知序列: x(n)= cos ( 0.4pin ) +cos(0.52pin)现基于有限个样本数来确定它的频谱。 a)画出x(n),0 ≤n≤10及其11点离散傅里叶变换。 b)画出x(n),0≤n≤10及其101点离散傅里叶变换。 c)画出x (n),0≤n≤100的离散傅里叶变换 d)根据a)、b)、c)的离散傅里叶变换结果,反算出各自的x(n) , 绘制三个结果并进行对比。
时间: 2024-02-18 17:58:52 浏览: 76
以下是用 MATLAB 实现以上要求的代码:
a) 11点离散傅里叶变换
```matlab
N = 11;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Sequence x(n)');
subplot(2,1,2);
stem(n,magX);
xlabel('k');
ylabel('|X(k)|');
title('Magnitude Spectrum of x(n)');
```
b) 101点离散傅里叶变换
```matlab
N = 101;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Sequence x(n)');
subplot(2,1,2);
stem(n,magX);
xlabel('k');
ylabel('|X(k)|');
title('Magnitude Spectrum of x(n)');
```
c) 0到100点离散傅里叶变换
```matlab
N = 101;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Sequence x(n)');
subplot(2,1,2);
stem((0:N-1)*(2*pi/N),magX);
xlabel('Normalized frequency(\times\pi rad/sample)');
ylabel('|X(k)|');
title('Magnitude Spectrum of x(n)');
```
d) 根据 a)、b)、c) 的离散傅里叶变换结果,反算出各自的 x(n),绘制三个结果并进行对比。
```matlab
% a) 11点离散傅里叶变换
N = 11;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
% 反傅里叶变换
x_recon = ifft(X);
figure;
subplot(2,2,1);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Sequence x(n)');
subplot(2,2,2);
stem(n,x_recon);
xlabel('n');
ylabel('x_{recon}(n)');
title('Reconstructed Sequence x_{recon}(n)');
% b) 101点离散傅里叶变换
N = 101;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
% 反傅里叶变换
x_recon = ifft(X);
subplot(2,2,3);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Sequence x(n)');
subplot(2,2,4);
stem(n,x_recon);
xlabel('n');
ylabel('x_{recon}(n)');
title('Reconstructed Sequence x_{recon}(n)');
% c) 0到100点离散傅里叶变换
N = 101;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
% 反傅里叶变换
x_recon = ifft(X);
figure;
subplot(2,2,1);
stem(n,x);
xlabel('n');
ylabel('x(n)');
title('Sequence x(n)');
subplot(2,2,2);
stem(n,x_recon);
xlabel('n');
ylabel('x_{recon}(n)');
title('Reconstructed Sequence x_{recon}(n)');
% 对比三个结果
N = 101;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
% 反傅里叶变换
x_recon_a = ifft(X);
N = 11;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
% 反傅里叶变换
x_recon_b = ifft(X);
subplot(2,2,3);
stem(n,x_recon_a);
hold on;
stem(n,x_recon_b);
xlabel('n');
ylabel('x_{recon}(n)');
title('Comparison of Reconstructed Sequences');
legend('101-point DFT','11-point DFT');
N = 101;
n = 0:N-1;
x = cos(0.4*pi*n) + cos(0.52*pi*n);
X = fft(x);
magX = abs(X);
phaseX = angle(X);
% 反傅里叶变换
x_recon_c = ifft(X);
subplot(2,2,4);
stem(n,x_recon_a);
hold on;
stem(n,x_recon_c);
xlabel('n');
ylabel('x_{recon}(n)');
title('Comparison of Reconstructed Sequences');
legend('101-point DFT','0-100 point DFT');
```
阅读全文