[英]increase / decrease the frequency of a signal using fft and ifft in matlab / octave
我正在嘗試使用fft和ifft增加/減少信號的頻率。 第一個圖是1hz,第二個圖是2hz,我試圖通過更改fft和ifft值來獲得 。
我可以在頻域和時域之間切換,但是如何使用fft / ifft增大或減小信號的頻率?
注意:是的,我知道我可以通過更改方程式的頻率值來更改頻率,但我只是將其用作測試信號。 我將使用的信號不會包含將要導入的方程式。
我試圖通過調整fft和ifft值獲得2hz的圖
下面的示例代碼:
clear all,clf
Fs = 100;% Sampling frequency
t=linspace(0,1,Fs);
%1a create signal
ya = .5*sin(2*pi*1*t);
%2a create frequency domain
ya_fft = fft(ya);
mag = abs(ya_fft);
phase = unwrap(angle(ya_fft));
ya_newifft=ifft(mag.*exp(i*phase));
%3a frequency back to time domain
ya_ifft=real(ifft(ya_fft));
%1b time domain plot
subplot(2,2,1),plot(t,ya)
title('1 Orginal Signal Time domain')
ylabel('amplitude')
xlabel('Seconds')
%2b frequency domain plot.
[xfreq,yamp]=rtplotfft(ya,Fs);
yamp2=(yamp(:,1)/max(abs(yamp(:,1)))*1); %keep at 1, amplitude levels adjustied in loop below
subplot(2,2,2),plot(xfreq,yamp)
title('2 Frequency domain')
xlabel('Frequency (Hz)')
ylabel('amplitude')
附:我正在使用與matlab一起使用的八度3.8.1
抱歉,以下內容有點混亂。 我手動完成所有操作,因為我不確定該怎么做。
首先,您需要了解MATLAB如何存儲頻域數據。 請看以下示例:
N = 100; % number of samples
Fs = 100; % sampling frequency
f = 5; % frequency of the signal
t = 0:1/N:1-1/N; % time goes from 0 to 1 second
y = cos(2*pi*f*t); % time signal
Y = fft(y); % frequency signal
figure(1); plot(y);
figure(2); plot(abs(Y));
Y(1)
是恆定偏移量(有時稱為DC偏移量) Y(2:N/2 + 1)
是一組正頻率 Y(N/2 + 2:end)
是負頻率的集合...通常,我們將在垂直軸的左側繪制該圖。 注意,因為N=100
是偶數,所以將有50
正頻率分量和49
負頻率分量。 如果N
為奇數,則正負頻率相等。
如果要增加頻率,我們需要做的是:
為了降低頻率,我們只需反轉移位的方向即可。
就您而言,您需要做的是...
clear all,clf
Fs = 100;% Sampling frequency
t=linspace(0,1,Fs);
%1a create signal
ya = .5*sin(2*pi*1*t);
%2a create frequency domain
ya_fft = fft(ya);
mag = abs(ya_fft);
phase = unwrap(angle(ya_fft));
ya_newifft=ifft(mag.*exp(i*phase));
% ----- changes start here ----- %
shift = 1; % shift amount
N = length(ya_fft); % number of points in the fft
mag1 = mag(2:N/2+1); % get positive freq. magnitude
phase1 = phase(2:N/2+1); % get positive freq. phases
mag2 = mag(N/2+2:end); % get negative freq. magnitude
phase2 = phase(N/2+2:end); % get negative freq. phases
% pad the positive frequency signals with 'shift' zeros on the left
% remove 'shift' components on the right
mag1s = [zeros(1,shift) , mag1(1:end-shift)];
phase1s = [zeros(1,shift) , phase1(1:end-shift)];
% pad the negative frequency signals with 'shift' zeros on the right
% remove 'shift' components on the left
mag2s = [mag2(shift+1:end), zeros(1,shift)];
phase2s = [phase2(shift+1:end), zeros(1,shift) ];
% recreate the frequency spectrum after the shift
% DC +ve freq. -ve freq.
magS = [mag(1) , mag1s , mag2s];
phaseS = [phase(1) , phase1s , phase2s];
x = magS.*cos(phaseS); % change from polar to rectangular
y = magS.*sin(phaseS);
ya_fft2 = x + i*y; % store signal as complex numbers
ya_ifft2 = real(ifft(ya_fft2)); % take inverse fft
plot(t,ya_ifft2); % time signal with increased frequency
然后你去了:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.