Matlab – Simple low/high filtering matlab

filterMATLAB

can someone help me with another matlab project:

Is it possible to create a simple low pass filter like in RC circuits? For instance if we create a sine wave like y=10*sin(2*pift).

Can i just filter the signal with a cutoff frequency to be able to see how the filtered signal looks like(for the amplitude decay)?

So when i enter some information for example f = cutoff and plot the filtered signal this must reduce the amplitude somewhere at 30% from the input signal ?

I've been looking for the functions like butter and cheby but they seem to do other kind of filtering like removing the noise from a signal. I just want some simple plots(input and output) which will show the principle of RC, RL low and high pass filters.

Best Answer

An RC circuit is a fist-order filter. To approximate a first order hardware filter, I generally use a IIR filter. A butterworth filter is usually my first choice for IIR, but for a first-order response, it doesn't really matter.

Here's an example with the cutoff frequency being the same as the signal frequency, so the filtered signal should be 3 dB down...

%first, make signal
fs = 1000;    %your sample rate, Hz
dur_sec = 1;  %what is the duration of your signal, seconds
t_sec = ([1:dur_sec*fs]-1)/fs;  %here is a vctor of time
freq_Hz = 25;       %what frequency do you want your sign wave
y = sin(2*pi*freq_Hz*t_sec);  %make your sine wave

%Second, make your filter
N = 1;  %first order
cutoff_Hz = 25;  %should be 3dB down at the cutoff
[b,a]=butter(N,cutoff_Hz/(fs/2),'lowpass');  %this makes a lowpass filter

%Third, apply the filter
y_filt = filter(b,a,y);

%Last, plot the results
figure;
plot(t_sec,y,t_sec,y_filt);
xlabel('Time (sec)');
ylabel('Amplitude');
ylim([-1 1]);
legend('Raw','Filtered');
title(['1st-Order Filter with Cutoff at ' num2str(cutoff_Hz) ' Hz']);

As an alternative to using the built-in filter design functions such as butter, you could choose model the circuit itself. A simple first order RC (or RL) circuit would result a first order differential equation. For an RC circuit, you'd then integrate the equation through time, given your sine wave as stimulation. That would work fine, but could be more of a hassle, depending upon your background.

For a simple, first-order hardware filter that is properly buffered by an op-amp on either end of the RC, I think that you'd find that the result using the first order butter filter is going to be awfully close (the same?) as modeling the circuit. The butter filter is way easier to implement in software (because i just gave you the code above), so I'd go that route.

When you move to 2nd order hardware filters, however, that's where you have to be more careful. You have a few options:

1) Continue to model your 2nd order hardware using one of the built-in filter functions. A second order butter is trivial to implement (alter the N in the code above), but this might not model the specific hardware filter that you've created. You'll have to choose the right kind of IIR filter to match the architecture of your hardware filter.

2) If you haven't picked your architecture for your hardware filter, you could choose the architecture to follow one of the canonical filter types, just so that it is easy to model via butter, cheby1, or whatever.

3) You could go back to modeling the circuit with differential equations. This would let you model any filter circuit, whether it followed a canonical type or not. You could also put non-linear effects in here, if you wanted.

For the first order RC filter, though, I think that any of the built-in filter types will be a decent enough model for an RC filter. I'd suggest that you play with my example code above. I think that it will satisfy your need.

Chip

Related Topic