![](/img/trans.png)
[英]Delay a signal in time domain with a phase change in the frequency domain after FFT
[英]Change phase of a signal in frequency domain (MatLab)
我在dsp.stackexchange上發布了這個問題,並被告知它與stackoverflow更相關,因為它主要是一個編程問題:
我正在嘗試編寫一個代碼,使我可以在頻域中更改信號的相位。 但是,我的輸出並不完全正確,因此一定有問題。 舉一個簡單的例子,假設我們具有函數y = sin(2 * pi * t),並且想要實現-pi / 2的相移。 我的代碼如下所示:
clear all
close all
N = 64; %number of samples
fs = 10; %sampling frequency
ts = 1/fs; %sample interval
tmax = (N-1)*ts;
t = 0:ts:tmax;
y = sin(2*pi*t);
figure
plot(t,y)
% We do the FT
f = -fs/2:fs/(N-1):fs/2;
Y = fftshift(fft(y));
% Magnitude spectrum
figure
plot(f,abs(Y));
phase = angle(Y);
% Phase spectrum
figure
plot(f,phase)
Y = ifftshift(Y)
% Attempt at phase shift
Y = Y.*exp(-i*2*pi*f*pi/2);
% Inverse FT
u = ifft(Y);
figure
plot(t,real(u))
除了最終圖如下所示,所有圖看起來都不錯:
這看起來幾乎是正確的,但並非完全正確。 如果有人可以給我一些如何糾正我的代碼以解決此問題的指示,我將不勝感激! 我感覺到我的錯誤與Y = Y.*exp(-i*2*pi*f*pi/2);
,但不確定如何解決。
我不能真正進入傅立葉分析的細節(因為我並不真正了解它們),但是我可以提供一些可行的解決方案並給出一些提示:
首先,您應該用想象的術語來表達您的觀點,即:
y = exp(1i*2*pi*t);
更重要的是,您必須真正改變相位,而不會弄亂整個頻譜:
% Attempt at phase shift
Y = abs(Y).*exp(1i*angle(Y)-1i*pi/4); % -pi/4 shift
您應該注意,該偏移不再與頻率相關,我認為這是有道理的。 最后,您可以繪制結果:
figure
plot(t,real(u),'k')
hold on
plot(t,real(y),'r')
real(y)
實際上是一個余弦函數,您從正弦開始,但希望您能理解。 對於pi / 4 shift,我得到了這樣的內容(從紅色開始,以黑色結束):
您在代碼設計中犯了3個主要錯誤。
我修改了您的代碼。 您將在下面找到它。 使用變量M可以更改輸入向量中正弦波的周期數。 在示例中,我設置了M = 3。
clear all;
close all;
T = 1; %length of sampling sequence in s
N = 64; %number of samples
M = 3; % number of periods per sequence
ts = T/N; %sample interval
fs = 1/ts %sampling frequency
tmax = (N-1)*ts;
t = 0:ts:tmax;
y = sin(2*pi*M*t);
fig01 = figure;
plot(t,y);
grid on;
%% We do the FT
Y = fft(y);
%% We create a frequency vector in natural order
% -fs/2, ..., 0, ... +fs/2
f =fftshift(( 0:(fs-1)) - fs/2);
%% Show Magnitude spectrum
% There shold be only two lines at -M and +M
figure;
plot(f,abs(Y),'o');
grid on;
%% Attempt at phase shift
% y/t) -> Y(w)
% y(t-t0) -> Y(w) * exp(-i*w*t0)
% Phase shift of pi/2 in frequncy domain is equavalent to as time shift
% of T/4 in time domain
Y = Y.*exp(-i*2*pi*f*T/4);
% Inverse FT
u = ifft(Y);
figure
hold on;
plot(t,real(u),'b-');
plot(t,real(y),'r-');
hold off;
grid;
具有三個正弦信號周期的輸入信號
輸入信號的頻譜。 頻率線為-3和+3
輸入信號(藍色)和相移信號(紅色)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.