1. 程式人生 > >【 MATLAB 】使用 MATLAB 得到高密度譜(補零得到DFT)和高解析度譜(獲得更多的資料得到DFT)的方式對比(附MATLAB指令碼)

【 MATLAB 】使用 MATLAB 得到高密度譜(補零得到DFT)和高解析度譜(獲得更多的資料得到DFT)的方式對比(附MATLAB指令碼)

那篇博文中,我們通過補零的方式來增加N,這樣最後的結論是隨著N的不斷增大,我們只會得到DTFT上的更多的取樣點,也就是說頻率取樣率增加了。通過補零,得到高密度譜(DFT),但不能得到高解析度譜,因為補零並沒有任何新的資訊附加到這個訊號上,要想得到高解析度譜,我們就得通過獲得更多的資料來進行求解DFT。

這篇博文就是為此而寫。

案例:

x(n) = cos(0.48\pi n)+cos(0.52\pi n)

想要基於有限樣本數來確定他的頻譜。

下面我們分如下幾種情況來分別討論:

a. 求出並畫出 x(n), 0 \leq n \leq 9 ,N = 10 的DFT以及DTFT;

b. 對上一問的x(n)通過補零的方式獲得區間[0,99]上的x(n),畫出 N = 100點的DFT,並畫出DTFT作為對比;

c.求出並畫出 x(n), 0 \leq n \leq 99 ,N = 100 的DFT以及DTFT;

d.對c問中的x(n)補零到N = 500,畫出 N = 500點的DFT,並畫出DTFT作為對比;

e. 比較c和d這兩個序列的序列的DFT以及DTFT的異同。

那就幹唄!

題解:

a.

clc;clear;close all;

n = 0:99;
x = cos(0.48*pi*n) + cos(0.52*pi*n);

n1 = 0:9;
y1 = x(1:10);

subplot(2,1,1)
stem(n1,y1);
title('signal x(n), 0 <= n <= 9');
xlabel('n');ylabel('x(n) over n in [0,9]');
Y1 = dft(y1,10);
magY1 = abs(Y1);
k1 = 0:1:9;
N = 10;
w1 = (2*pi/N)*k1;
subplot(2,1,2);
stem(w1/pi,magY1);
title('DFT of x(n) in [0,9]');
xlabel('frequency in pi units');

%In order to clearly see the relationship between DTFT and DFT, we draw DTFT on the same picture.


%Discrete-time Fourier Transform
K = 500;
k = 0:1:K;
w = 2*pi*k/K; %plot DTFT in [0,2pi];
X = y1*exp(-j*n1'*w);

magX = abs(X);
hold on 
plot(w/pi,magX);

hold off

b.

clc;clear;close all;

n = 0:99;
x = cos(0.48*pi*n) + cos(0.52*pi*n);

% zero padding into N = 100
n1 = 0:99;
y1 = [x(1:10),zeros(1,90)];

subplot(2,1,1)
stem(n1,y1);
title('signal x(n), 0 <= n <= 99');
xlabel('n');ylabel('x(n) over n in [0,99]');
Y1 = dft(y1,100);
magY1 = abs(Y1);
k1 = 0:1:99;
N = 100;
w1 = (2*pi/N)*k1;
subplot(2,1,2);
stem(w1/pi,magY1);
title('DFT of x(n) in [0,9]');
xlabel('frequency in pi units');

%In order to clearly see the relationship between DTFT and DFT, we draw DTFT on the same picture.


%Discrete-time Fourier Transform
K = 500;
k = 0:1:K;
w = 2*pi*k/K; %plot DTFT in [0,2pi];
X = y1*exp(-j*n1'*w);
% w = [-fliplr(w),w(2:K+1)];   %plot DTFT in [-pi,pi]
% X = [fliplr(X),X(2:K+1)];    %plot DTFT in [-pi,pi]
magX = abs(X);
% angX = angle(X)*180/pi;
% figure
% subplot(2,1,1);
hold on 
plot(w/pi,magX);
% title('Discrete-time Fourier Transform in Magnitude Part');
% xlabel('w in pi units');ylabel('Magnitude of X');
% subplot(2,1,2);
% plot(w/pi,angX);
% title('Discrete-time Fourier Transform in Phase Part');
% xlabel('w in pi units');ylabel('Phase of X ');
hold off


c.

clc;clear;close all;

n = 0:99;
x = cos(0.48*pi*n) + cos(0.52*pi*n);

% n1 = 0:99;
% y1 = [x(1:10),zeros(1,90)];

subplot(2,1,1)
stem(n,x);
title('signal x(n), 0 <= n <= 99');
xlabel('n');ylabel('x(n) over n in [0,99]');
Xk = dft(x,100);
magXk = abs(Xk);
k1 = 0:1:99;
N = 100;
w1 = (2*pi/N)*k1;
subplot(2,1,2);
stem(w1/pi,magXk);
title('DFT of x(n) in [0,99]');
xlabel('frequency in pi units');

%In order to clearly see the relationship between DTFT and DFT, we draw DTFT on the same picture.


%Discrete-time Fourier Transform
K = 500;
k = 0:1:K;
w = 2*pi*k/K; %plot DTFT in [0,2pi];
X = x*exp(-j*n'*w);
% w = [-fliplr(w),w(2:K+1)];   %plot DTFT in [-pi,pi]
% X = [fliplr(X),X(2:K+1)];    %plot DTFT in [-pi,pi]
magX = abs(X);
% angX = angle(X)*180/pi;
% figure
% subplot(2,1,1);
hold on 
plot(w/pi,magX);
% title('Discrete-time Fourier Transform in Magnitude Part');
% xlabel('w in pi units');ylabel('Magnitude of X');
% subplot(2,1,2);
% plot(w/pi,angX);
% title('Discrete-time Fourier Transform in Phase Part');
% xlabel('w in pi units');ylabel('Phase of X ');
hold off


太小了,放大看:

d.

clc;clear;close all;

n = 0:99;
x = cos(0.48*pi*n) + cos(0.52*pi*n);

% n1 = 0:99;
% y1 = [x(1:10),zeros(1,90)];

%zero padding into N = 500
n1 = 0:499;
x1 = [x,zeros(1,400)];

subplot(2,1,1)
stem(n1,x1);
title('signal x(n), 0 <= n <= 499');
xlabel('n');ylabel('x(n) over n in [0,499]');
Xk = dft(x1,500);
magXk = abs(Xk);
k1 = 0:1:499;
N = 500;
w1 = (2*pi/N)*k1;
subplot(2,1,2);
% stem(w1/pi,magXk);
stem(w1/pi,magXk);
title('DFT of x(n) in [0,499]');
xlabel('frequency in pi units');

%In order to clearly see the relationship between DTFT and DFT, we draw DTFT on the same picture.


%Discrete-time Fourier Transform
K = 500;
k = 0:1:K;
w = 2*pi*k/K; %plot DTFT in [0,2pi];
X = x1*exp(-j*n1'*w);
% w = [-fliplr(w),w(2:K+1)];   %plot DTFT in [-pi,pi]
% X = [fliplr(X),X(2:K+1)];    %plot DTFT in [-pi,pi]
magX = abs(X);
% angX = angle(X)*180/pi;
% figure
% subplot(2,1,1);
hold on 
plot(w/pi,magX,'r');
% title('Discrete-time Fourier Transform in Magnitude Part');
% xlabel('w in pi units');ylabel('Magnitude of X');
% subplot(2,1,2);
% plot(w/pi,angX);
% title('Discrete-time Fourier Transform in Phase Part');
% xlabel('w in pi units');ylabel('Phase of X ');
hold off


e.

clc;clear;close all;

n = 0:99;
x = cos(0.48*pi*n) + cos(0.52*pi*n);

subplot(2,1,1)
% stem(n,x);
% title('signal x(n), 0 <= n <= 99');
% xlabel('n');ylabel('x(n) over n in [0,99]');
Xk = dft(x,100);
magXk = abs(Xk);
k1 = 0:1:99;
N = 100;
w1 = (2*pi/N)*k1;
stem(w1/pi,magXk);
title('DFT of x(n) in [0,99]');
xlabel('frequency in pi units');

%In order to clearly see the relationship between DTFT and DFT, we draw DTFT on the same picture.


%Discrete-time Fourier Transform
K = 500;
k = 0:1:K;
w = 2*pi*k/K; %plot DTFT in [0,2pi];
X = x*exp(-j*n'*w);
% w = [-fliplr(w),w(2:K+1)];   %plot DTFT in [-pi,pi]
% X = [fliplr(X),X(2:K+1)];    %plot DTFT in [-pi,pi]
magX = abs(X);
% angX = angle(X)*180/pi;
% figure
% subplot(2,1,1);
hold on 
plot(w/pi,magX);
% title('Discrete-time Fourier Transform in Magnitude Part');
% xlabel('w in pi units');ylabel('Magnitude of X');
% subplot(2,1,2);
% plot(w/pi,angX);
% title('Discrete-time Fourier Transform in Phase Part');
% xlabel('w in pi units');ylabel('Phase of X ');
hold off





% clc;clear;close all;
% 
% n = 0:99;
% x = cos(0.48*pi*n) + cos(0.52*pi*n);

% n1 = 0:99;
% y1 = [x(1:10),zeros(1,90)];

%zero padding into N = 500
n1 = 0:499;
x1 = [x,zeros(1,400)];
subplot(2,1,2);
% subplot(2,1,1)
% stem(n1,x1);
% title('signal x(n), 0 <= n <= 499');
% xlabel('n');ylabel('x(n) over n in [0,499]');
Xk = dft(x1,500);
magXk = abs(Xk);
k1 = 0:1:499;
N = 500;
w1 = (2*pi/N)*k1;

stem(w1/pi,magXk);
title('DFT of x(n) in [0,499]');
xlabel('frequency in pi units');

%In order to clearly see the relationship between DTFT and DFT, we draw DTFT on the same picture.


%Discrete-time Fourier Transform
K = 500;
k = 0:1:K;
w = 2*pi*k/K; %plot DTFT in [0,2pi];
X = x1*exp(-j*n1'*w);
% w = [-fliplr(w),w(2:K+1)];   %plot DTFT in [-pi,pi]
% X = [fliplr(X),X(2:K+1)];    %plot DTFT in [-pi,pi]
magX = abs(X);
% angX = angle(X)*180/pi;
% figure
% subplot(2,1,1);
hold on 
plot(w/pi,magX,'r');
% title('Discrete-time Fourier Transform in Magnitude Part');
% xlabel('w in pi units');ylabel('Magnitude of X');
% subplot(2,1,2);
% plot(w/pi,angX);
% title('Discrete-time Fourier Transform in Phase Part');
% xlabel('w in pi units');ylabel('Phase of X ');
hold off

區域性放大看: