简体   繁体   English

Matlab中的傅立叶变换和LTI滤波器以及频率响应

[英]Fourier transform and LTI filter and frequency response in Matlab

I'm new to Matlab for LTI signal processing and wondering if anyone can help with something that I'm sure is meant to be basic. 我是Matlab的LTI信号处理新手,想知道是否有人可以提供我确定必不可少的帮助。 I've spent hours and hours researching and obtaining background information and still cannot obtain a clear path to tackle these problems. 我花了很多时间研究和获取背景信息,但仍然无法找到解决这些问题的明确途径。 So far, from scratch, I have generated a signal required and managed to use the fft function to produce the signal's DFT: 到目前为止,从零开始,我已经生成了所需的信号,并设法使用fft函数来生成信号的DFT:

function x = fourier_rikki(A,t,O)

Fs = 1000;
t = 0:(1/Fs):1;

A = [0.5,0,0.5];
N = (length(A) - 1)/2;
x = zeros(size(t));
f1 = 85;
O1 = 2*pi*f1;

for k = 1:length(A)
x1 = x + A(k)*exp(1i*O1*t*(k-N-1));
end

f2 = 150;
O2 = 2*pi*f2;

for k = 1:length(A);
x2 = x + A(k)*exp(1i*O2*t*(k-N-1));
end

f3 = 330;
O3 = 2*pi*f3;

for k = 1:length(A);
x3 = x + A(k)*exp(1i*O3*t*(k-N-1));
end

signal = x1 + x2 + x3;

figure(1);
subplot(3,1,1);
plot(t, signal);
title('Signal x(t) in the Time Domain');
xlabel('Time (Seconds)');
ylabel('x(t)');

X = fft(signal);  %DFT of the signal

subplot(3,1,2);
plot(t, X);
title('Power Spectrum of Discrete Fourier Transform of x(t)');
xlabel('Time (Seconds)');
ylabel('Power');

f = linspace(0, 1000, length(X)); %?

subplot(3,1,3);
plot(f, abs(X));  %Only want the positive values
title('Spectral Frequency');
xlabel('Frequency (Hz)'); ylabel('Power');

end

At this stage, I'm assuming this is correct for: 在此阶段,我假设这是正确的:

"Generate a signal with frequencies 85,150,330Hz using a sampling frequency of 1000Hz - plot 1seconds worth of the signal and its Discrete Fourier Transform." “使用1000Hz的采样频率生成频率为85,150,330Hz的信号-绘制价值1秒的信号及其离散傅立叶变换。”

The next step is to "Find the frequency response of an LTI system that filters out the higher and lower frequencies using the Fourier Transform". 下一步是“查找使用傅立叶变换过滤掉较高和较低频率的LTI系统的频率响应”。 I'm stuck trying to create an LTI system that does that! 我一直试图创建一个可以做到这一点的LTI系统! I have to be left with the 150Hz signal, and I'm guessing I perform the filtering on the FFT, perhaps using conv. 我必须留有150Hz信号,我猜想我可能会使用conv对FFT进行滤波。

My course is not a programming course - we are not assessed on our programming skills and I have minimal Matlab experience - basically we have been left to our own devices to struggle through, so any help would be greatly appreciated! 我的课程不是编程课程-我们没有编程技能方面的评估,并且我对Matlab的经验很少-基本上,我们任凭自己的设备苦苦挣扎,因此,我们将不胜感激! I am sifting through tonnes of different examples and searching Matlab functions using 'help' etc, but since each one is different and does not have a break down of the variables used, explaining why certain parameters/values are chosen etc. it is just adding to the confusion. 我正在筛选大量不同的示例,并使用“帮助”等搜索Matlab函数,但是由于每个示例都不相同,并且没有使用的变量分类,因此说明了为什么选择某些参数/值等的原因,这只是添加混乱。

Among many (many) others I have looked at: http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html http://gribblelab.org/scicomp/09_Signals_and_sampling.html section 10.4 especially. 我看过的许多其他工具包括: http : //www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html http://gribblelab.org/scicomp/09_Signals_and_sampling.html尤其是第10.4节。 As well as Matlab Geeks examples and Mathworks Matlab function explanations. 以及Matlab Geeks示例和Mathworks Matlab函数说明。 I guess the worst that can happen is that nobody answers and I continue burning my eyeballs out until I manage to come up with something :) Thanks in advance. 我猜可能发生的最糟糕的情况是,没有人回答,我继续竭尽全力,直到我设法提出建议:)预先感谢。

I found this bandpass filter code as a Mathworks example, which is exactly what needs to be applied to my fft signal, but I don't understand the attenuation values Ast or the amount of ripple Ap. 我发现此带通滤波器代码作为Mathworks示例,正是我的fft信号需要使用的代码,但我不了解衰减值Ast或纹波量Ap。

n = 0:159;
x = cos(pi/8*n)+cos(pi/2*n)+sin(3*pi/4*n);

d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',1/4,3/8,5/8,6/8,60,1,60);
Hd = design(d,'equiripple');

y = filter(Hd,x);
freq = 0:(2*pi)/length(x):pi;
xdft = fft(x);
ydft = fft(y);

plot(freq,abs(xdft(1:length(x)/2+1)));
hold on;
plot(freq,abs(ydft(1:length(x)/2+1)),'r','linewidth',2);
legend('Original Signal','Bandpass Signal');

Here is something you can use as a reference. 这是您可以用作参考的东西。 I think I got the gist of what you were trying to do. 我想我知道您要做什么。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

clear all
close all

Fs = 1000;
t = 0:(1/Fs):1;
N = length(t);

% 85, 150, and 330 Hz converted to radian frequency
w1 = 2*pi*85;
w2 = 2*pi*150;
w3 = 2*pi*330;

% amplitudes
a1 = 1;
a2 = 1.5;
a3 = .75;

% construct time-domain signals
x1 = a1*cos(w1*t);
x2 = a2*cos(w2*t);
x3 = a3*cos(w3*t);

% superposition of 85, 150, and 330 Hz component signals
x = x1 + x2 + x3; 

figure
plot(t(1:100), x(1:100));
title('unfiltered time-domain signal, amplitude vs. time');
ylabel('amplitude');
xlabel('time (seconds)');

% compute discrete Fourier transform of time-domain signal
X = fft(x); 
Xmag = 20*log10(abs(X)); % magnitude spectrum
Xphase = 180*unwrap(angle(X))./pi; % phase spectrum (degrees)
w = 2*pi*(0:N-1)./N; % normalized radian frequency
f = w./(2*pi)*Fs; % radian frequency to Hz
k = 1:N; % bin indices 

% plot magnitude spectrum
figure
plot(f, Xmag)
title('frequency-domain signal, magnitude vs. frequency');
xlabel('frequency (Hz)');
ylabel('magnitude (dB)');

% frequency vector of the filter. attenuates undesired frequency components
% and keeps desired components. 
H = 1e-3*ones(1, length(k));
H(97:223) = 1;
H((end-223):(end-97)) = 1;

% plot magnitude spectrum of signal and filter
figure
plot(k, Xmag)
hold on
plot(k, 20*log10(H), 'r')
title('frequency-domain signal (blue) and filter (red), magnitude vs. bin index');
xlabel('bin index');
ylabel('magnitude (dB)');

% filtering in frequency domain is just multiplication
Y = X.*H;

% plot magnitude spectrum of filtered signal
figure
plot(f, 20*log10(abs(Y)))
title('filtered frequency-domain signal, magnitude vs. frequency');
xlabel('frequency (Hz)');
ylabel('magnitude (dB)');

% use inverse discrete Fourier transform to obtain the filtered time-domain
% signal. This signal is complex due to imperfect symmetry in the
% frequency-domain, however the imaginary components are nearly zero.
y = ifft(Y);

% plot overlay of filtered signal and desired signal
figure
plot(t(1:100), x(1:100), 'r')
hold on
plot(t(1:100), x2(1:100), 'linewidth', 2)
plot(t(1:100), real(y(1:100)), 'g')
title('input signal (red), desired signal (blue), signal extracted via filtering (green)');
ylabel('amplitude');
xlabel('time (seconds)');

Here is the end result... 这是最终结果... 在此处输入图片说明

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

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