Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stateful filtering fails with multi-dim array #372

Open
hergum opened this issue Aug 17, 2020 · 3 comments
Open

Stateful filtering fails with multi-dim array #372

hergum opened this issue Aug 17, 2020 · 3 comments

Comments

@hergum
Copy link

hergum commented Aug 17, 2020

Using more than one dimension in the input array makes filt fail for stateful filter.

s = rand(10,3)
df = digitalfilter(Lowpass(0.25), Butterworth(4))
f = DF2TFilter(df)
filt(f,s)

ERROR: MethodError: no method matching filt(::DF2TFilter{SecondOrderSections{Float64,Float64},Array{Float64,2}}, ::Array{Float64,2})

One-dimensional input works as expected. Two-dimensional inputs works along the first dimension, as expected, for stateless filtering filt(df,s).

Any ideas on how to make this work according to the doc?

@galenlynch
Copy link
Member

You're right that the docs suggest that this should work. However, that doc string was written for normal filter coefficients, and not stateful filters. It's not really clear to me what the expected behavior would be when a stateful filter is applied to a matrix. For stateless filtering each column is treated as a separate signal, where the filter state is reset at the start of each column of the signal, and the filter state is discarded at the end of each column. If you were to do the same with a stateful filter, it's not clear what the filter state should be either at the start of each column, or after all the columns have been filtered (i.e. which column of the signal should the state reflect).

Depending on what you are expecting the filter state to do between columns of your signal, you could write an explicit loop accordingly. The only thing that really makes sense to me, given your decision to use a stateful filter, is that each column is a continuation of the same signal. In this case, there is no reason to use a matrix instead of a vector. If you instead want to maintain separate filter state for each column, then you should make separate filter objects for each column.

Could you explain your use case a little bit more? Are you processing very long signals in batches, or are you doing some on-line filtering of signals? Are you filtering multiple channels of data that should be treated separately? What is the reason that you want to use stateful filters in the first place?

@hergum
Copy link
Author

hergum commented Aug 17, 2020

I would like the stateful filter to keep track of the state of the filter for each column.

My use-case is I get long 2D signals in batches. Each row is independent, and all rows should be processed the same way. The rows are continuous in time, and must be processed as if I had the full signal available, so I need to preserve the filter state. Its for a medical device, but the use case might as well be wanting to low-pass filter the audio track of 15 radio stations. In matlab I'm used to doing things like:

>> [b,a]=butter(4,0.25);
>> [s,state]=filter(b,a,rand(15,800),[],2); % [] means no state for the first batch
>> size(state)

ans =

     4    15
>> [s,state]=filter(b,a,rand(15,800),state,2);  % use the filter state from the end of last batch to initialize the next

The documentation suggested I could do this very conveniently in Julia with a stateful filter. I'll do it in a loop instead.

@galenlynch
Copy link
Member

galenlynch commented Aug 18, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants