Using the Lowpass filter to properly filter data without a time variable

Hello,
I am having some issues getting the Lowpass filter to work on my data. I have an x and y variable where x is the inputs and y is the outputs, but they are not related by a function and I am not interesed in plotting time, which I know is generally used with the Lowpass filter. I have tried many different variations of code for the Lowpass and have gotten graphs that appear to be working but when I go to lower the frequency to get rid of the noise, nothing happens. Meaning my graph with a cutoff frequency of 1 looks the same as my graph with a cutoff frequency of 0.001. Is anyone able to explain why this might be or how I might fix it? As the graph still looks farily noisy and I would like it to be a smoother curve. For reference my Fs value is 12, x and y are two fairly long columns of data, and the FilterOrder I have changed around a lot but it doesn't seem to change my plot.
Fs=12;
d=designfilt('lowpassfir','FilterOrder',5,'cutoffFrequency',0.01,'SampleRate',Fs)
k=filter(d,x);
p=filer(d,y);
plot(k,p)
I have also tried some options where the the cutoff frequency does seem to change above 1, so this is not the most helpful to me. It will also not let me plot my two variables, it seems to only want to use time/Hertz. Maybe this is my main issue? I tried what is below as well for this but it does not seem to be doing what I want it to. I also tried incorportating a smoothingspline to help relate the varibales but this does not really seem to be working (although this code did not give me an error message).
Any help would be appreciated!
f=fit(x,y,'smoothingspline');
plot(f,x,y,'b')
Fs=12;
k=f(x);
p=lowpass(k,0.01,fs);
plot(x,p,'r')

 Respuesta aceptada

It is always best to use the freqz function to see what the filter is actually doing. (I find fvtool a bit difficult to work with.)
Fs=12;
d=designfilt('lowpassfir','FilterOrder',5,'cutoffFrequency',0.01,'SampleRate',Fs)
d =
digitalFilter with properties: Coefficients: [0.0288 0.1431 0.3281 0.3281 0.1431 0.0288] Specifications: FrequencyResponse: 'lowpass' ImpulseResponse: 'fir' SampleRate: 12 FilterOrder: 5 CutoffFrequency: 0.0100 DesignMethod: 'window' Use fvtool to visualize filter Use designfilt to edit filter Use filter to filter data
figure
freqz(d.Coefficients, 1, 2^14, Fs)
Experiment to see if increasing the order of the filter improves its performance:
Fs=12;
d=designfilt('lowpassfir','FilterOrder',45,'cutoffFrequency',0.01,'SampleRate',Fs)
d =
digitalFilter with properties: Coefficients: [1×46 double] Specifications: FrequencyResponse: 'lowpass' ImpulseResponse: 'fir' SampleRate: 12 FilterOrder: 45 CutoffFrequency: 0.0100 DesignMethod: 'window' Use fvtool to visualize filter Use designfilt to edit filter Use filter to filter data
figure
freqz(d.Coefficients, 1, 2^14, Fs)
.

6 comentarios

@Star Strider thank you for responding! Sorry, I have never used that function before, but when I type in the code it says that it does not recognize the "d.Coefficients" term, should I not just be running that or is there a different way to do it? I also adjusted the order quite a bit and that seemed to help, thank you! But can you explain why it takes that data and goes from 0 to say 100 (with order 100), then the usable plot begins after 100? Is there a way to change this?
My pleasure!
Since you are runnning R2021a, there should be no problems with running my code. I have no problems running it either here or on my own computer (with R2021a).
Please copy and paste all the red text of the error from your Command Window to a Comment here.
Also, I do not understand ‘the usable plot begins after 100’. This code designs a lowpass filter by default (it can design other filters if instructed to do so), so the passband will be between from 0 to 0.01 times the sampling frequency (or 0.12 Hz here), and the stopband from 0.12 Hz to the Nyquist frequency, 6 Hz here.
.
Hi @Star Strider, thank you for getting back to me again!
The error message I'm getting reads:
(sorry not copying red for some reason)
Unable to resolve the name d.Coefficients
Error in LowPass (line 7)
freqz(d.Coefficients, 1, 2^14, Fs)
In reference to the second part, my y value data is all between the values of 7 and 7.5, and when I set a number for FilterOrder the graph after the filter is applied the data will slope from 0 to the normal range of data within the number of points with the FilterOrder. For example, if I set the FilterOrder to 100, my y data will slope from 0 to that usable range in the first 100 x values. Therefore making the first 100 data poins not very usable. Is there a way to fix this or is this just how the filter works?
Thanks again!
My pleasure!
I have no idea what is causing the problem with ‘d.Coefficients’. Did you copy and run the code I posted exactly as I wrote it, including the designfilt call? (The designfilt call creates ‘d.Coefficients’.) It should run without error in your R2021a as it did here and on my computer (R2021a).
The ‘first 100 data points’ problem may be the result of using the filter function. I encourage you to use the filtfilt function, instead of filter, with the ‘d’ filter and with all other filters, both FIR and IIR. That will likely address the problem with the first 100 data points.
.
@Star Strider I am not sure where the error is coming from, but the filtfilt worked great thank you so much, that will work perfectly for what I'm looking for!
Thank you again.
As always, my pleasure!
.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2021a

Preguntada:

el 14 de Jul. de 2021

Comentada:

el 15 de Jul. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by