如何在 Matlab 中更改信号范围的值
How to change value of a signal's range in Matlab
假设我在 Matlab 中有这样的信号
x = cos(2*pi*10*t) + cos(2*pi*20*t) + cos(2*pi*50*t);
我想将 20 到 30 赫兹之间的值更改为 0。我该怎么做?我的意思是,那些从 x 公式生成的值,我想稍微改变一下。
您可以使用滤波器,也可以通过进入傅里叶 space 并明确将信号的频率设置为零来自行过滤。之后,您需要回到时域。这是一个代码:
t=0:0.01:0.99; % time
x = cos(2*pi*10*t) + cos(2*pi*20*t) + cos(2*pi*50*t); %signal
xf=fftshift(fft(x)); %Fourier signal
N=size(x,2); % Size of the signal
frequency=2*pi*[-N/2:N/2-1]; %frequency range
frequencyrangeplus=find(frequency/(2*pi)>=20 & frequency/(2*pi)<=30); %find positive frequencies in the required range
frequencyrangeminus=find(frequency/(2*pi)<=-20 & frequency/(2*pi)>=-30); %find negative frequencies in the required range
xf(frequencyrangeplus)=0; %set signal to zero at positive frequencies range
xf(frequencyrangeminus)=0; %set signal to zero at nagative frequencies range
xnew=ifft(ifftshift(xf)); %get the new signal in time domain
xcheck= cos(2*pi*10*t) + cos(2*pi*50*t); % to check the code
max(abs(xcheck-xnew)) % maximum difference
您可以通过对 x 执行 FFT 并将 20 到 30 Hz 之间的那些值设置为零,然后对先前的值应用 FFT 逆运算来实现,您应该得到没有这些频率的信号。但是,您可能会丢失有价值的信息,或者信号可能看起来不像您希望的那样。因此,我推荐你使用"Bandstop filter"。带阻滤波器将接收截止频率(您要使用的限制频率)和一些其他参数。带阻滤波器基本上从信号中移除您指定的频率。好的部分是它可以像执行以下操作一样简单地完成:
首先你必须构建过滤器。为此,您需要指定可以根据需要定义的过滤器顺序。通常二阶效果很好。此外,您必须了解您的采样率 Fs。
d = designfilt('bandstopiir','FilterOrder',2, ...
'HalfPowerFrequency1',20,'HalfPowerFrequency2',30, ...
'SampleRate',Fs);
现在您只需将滤波器应用于您想要的信号。
filtered_signal_x = filtfilt(d, x)
现在,filtered_signal_x 不应该有您要删除的频率。通过使用带阻器,您不必弄乱 FFT 之类的东西,而且速度更快,所以我认为这是最好的选择。
假设我在 Matlab 中有这样的信号
x = cos(2*pi*10*t) + cos(2*pi*20*t) + cos(2*pi*50*t);
我想将 20 到 30 赫兹之间的值更改为 0。我该怎么做?我的意思是,那些从 x 公式生成的值,我想稍微改变一下。
您可以使用滤波器,也可以通过进入傅里叶 space 并明确将信号的频率设置为零来自行过滤。之后,您需要回到时域。这是一个代码:
t=0:0.01:0.99; % time
x = cos(2*pi*10*t) + cos(2*pi*20*t) + cos(2*pi*50*t); %signal
xf=fftshift(fft(x)); %Fourier signal
N=size(x,2); % Size of the signal
frequency=2*pi*[-N/2:N/2-1]; %frequency range
frequencyrangeplus=find(frequency/(2*pi)>=20 & frequency/(2*pi)<=30); %find positive frequencies in the required range
frequencyrangeminus=find(frequency/(2*pi)<=-20 & frequency/(2*pi)>=-30); %find negative frequencies in the required range
xf(frequencyrangeplus)=0; %set signal to zero at positive frequencies range
xf(frequencyrangeminus)=0; %set signal to zero at nagative frequencies range
xnew=ifft(ifftshift(xf)); %get the new signal in time domain
xcheck= cos(2*pi*10*t) + cos(2*pi*50*t); % to check the code
max(abs(xcheck-xnew)) % maximum difference
您可以通过对 x 执行 FFT 并将 20 到 30 Hz 之间的那些值设置为零,然后对先前的值应用 FFT 逆运算来实现,您应该得到没有这些频率的信号。但是,您可能会丢失有价值的信息,或者信号可能看起来不像您希望的那样。因此,我推荐你使用"Bandstop filter"。带阻滤波器将接收截止频率(您要使用的限制频率)和一些其他参数。带阻滤波器基本上从信号中移除您指定的频率。好的部分是它可以像执行以下操作一样简单地完成:
首先你必须构建过滤器。为此,您需要指定可以根据需要定义的过滤器顺序。通常二阶效果很好。此外,您必须了解您的采样率 Fs。
d = designfilt('bandstopiir','FilterOrder',2, ... 'HalfPowerFrequency1',20,'HalfPowerFrequency2',30, ... 'SampleRate',Fs);
现在您只需将滤波器应用于您想要的信号。
filtered_signal_x = filtfilt(d, x)
现在,filtered_signal_x 不应该有您要删除的频率。通过使用带阻器,您不必弄乱 FFT 之类的东西,而且速度更快,所以我认为这是最好的选择。