简体   繁体   English

在Matlab /八度中使用fft和ifft增加/降低信号的频率

[英]increase / decrease the frequency of a signal using fft and ifft in matlab / octave

I'm trying to increase / decrease the frequency of a signal using fft and ifft. 我正在尝试使用fft和ifft增加/减少信号的频率。 The first plot is 1hz and the second plot is 2hz which I'm trying to get by altering the fft and ifft values . 第一个图是1hz,第二个图是2hz,我试图通过更改fft和ifft值来获得

I can go between the frequency domain and time domain but how can I increase or decrease the frequency of the signal using fft / ifft? 我可以在频域和时域之间切换,但是如何使用fft / ifft增大或减小信号的频率?

Note: Yes I know I could change the frequency by changing the frequency value of the equation but I'm just using that as a test signal. 注意:是的,我知道我可以通过更改方程式的频率值来更改频率,但我只是将其用作测试信号。 The signals I will be using won't have equations they will be imported. 我将使用的信号不会包含将要导入的方程式。

情节1hz

The plot with 2hz is what I'm trying to get by adjusting the fft and ifft values 我试图通过调整fft和ifft值获得2hz的图

我试图通过调整fft和ifft得到的图2hz

Example code below: 下面的示例代码:

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: I'm using octave 3.8.1 which works with matlab 附:我正在使用与matlab一起使用的八度3.8.1

I apologize that the following is a bit messy. 抱歉,以下内容有点混乱。 I did everything manually because I'm not sure how else to do it. 我手动完成所有操作,因为我不确定该怎么做。

First you need to know how MATLAB stores frequency domain data. 首先,您需要了解MATLAB如何存储频域数据。 Take the following example: 请看以下示例:

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) is the constant offset (sometimes called the DC offset) Y(1)是恒定偏移量(有时称为DC偏移量)
  • Y(2:N/2 + 1) is the set of positive frequencies Y(2:N/2 + 1)是一组正频率
  • Y(N/2 + 2:end) is the set of negative frequencies... normally we would plot this left of the vertical axis. Y(N/2 + 2:end)是负频率的集合...通常,我们将在垂直轴的左侧绘制该图。

Note that because N=100 is even, there will be 50 positive frequency components and 49 negative frequency components. 注意,因为N=100是偶数,所以将有50正频率分量和49负频率分量。 If N is odd, then there will be an equal number of positive and negative frequencies. 如果N为奇数,则正负频率相等。

If we want to increase the frequency, we need to do is: 如果要增加频率,我们需要做的是:

  • take the fourier transform 进行傅立叶变换
  • leave the DC offset unchanged 保持直流偏移不变
  • shift the positive part of the spectrum to the right 将频谱的正向右移
  • shift the negative part of the spectrum to the left 将频谱的负部分向左移动

To decrease the frequency we would just reverse the direction of the shifts. 为了降低频率,我们只需反转移位的方向即可。

In your case, what you need to do is... 就您而言,您需要做的是...

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

And there you go: 然后你去了:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM