在matlab / octave中使用fft和ifft增加/减少信号的频率

时间:2014-12-30 20:24:11

标签: matlab fft octave ifft

我尝试使用fft和ifft来增加/减少信号的频率。第一个图是 1hz,第二个图是2hz,我试图通过改变fft和ifft值来获得

我可以介于频域和时域之间,但如何使用fft / ifft增加或减少信号的频率?

注意:是的我知道我可以通过改变等式的频率值来改变频率,但我只是将其用作测试信号。我将使用的信号不会有导入它们的方程式。

Plot 1hz

我想通过调整fft和ifft值来获得2hz的情节

Plot 2hz which I trying to get by adjusting the fft and ifft

以下示例代码:

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')

Ps:我使用octave 3.8.1,它与matlab一起使用

1 个答案:

答案 0 :(得分:2)

我道歉以下有点乱。我手动完成所有操作,因为我不确定该怎么做。

首先,您需要了解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)是负频率的集合...通常我们会绘制垂直轴的 left

请注意,因为N=100是偶数,所以会有50个正频率分量和49个负频率分量。如果N是奇数,则会有相同数量的正负频率。

如果我们想增加频率,我们需要做的是:

  • 采取傅立叶变换
  • 保持DC偏移不变
  • 将光谱的正面部分移到右边
  • 将光谱的负片部分移到左侧

为了降低频率,我们只会改变转变的方向。

在您的情况下,您需要做的是......

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

然后你去了:

enter image description here