[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <2025081409-ubiquitous-cuckoo-76acbd@boujee-and-buff>
Date: Thu, 14 Aug 2025 09:38:02 -0400
From: Ben Collins <bcollins@...nel.org>
To: David Lechner <dlechner@...libre.com>,
Ben Collins <bcollins@...ter.com>, Jonathan Cameron <jic23@...nel.org>,
Nuno Sá <nuno.sa@...log.com>, Andy Shevchenko <andy@...nel.org>, linux-iio@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 5/5] iio: mcp9600: Add support for IIR filter
On Thu, Aug 14, 2025 at 09:06:39AM -0500, Ben Collins wrote:
> On Wed, Aug 13, 2025 at 05:52:04PM -0500, David Lechner wrote:
> > On 8/13/25 10:15 AM, Ben Collins wrote:
> > > MCP9600 supports an IIR filter with 7 levels. Add IIR attribute
> ...
> > > static int mcp9600_read(struct mcp9600_data *data,
> > > @@ -186,6 +189,9 @@ static int mcp9600_read_raw(struct iio_dev *indio_dev,
> > > case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
> > > *val = mcp9600_tc_types[data->thermocouple_type];
> > > return IIO_VAL_CHAR;
> > > + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
> > > + *val = data->filter_level;
> >
> > We can't just pass the raw value through for this. The ABI is defined
> > in Documentation/ABI/testing/sysfs-bus-iio and states that the value
> > is the frequency in Hz.
> ...
> > For example, for 3 Hz sample rate (18-bit samples), I got:
> >
> > n f_3dB (Hz)
> > 1 0.58774
> > 2 0.24939
> > 3 0.12063
> > 4 0.05984
> > 5 0.02986
> > 6 0.01492
> > 7 0.00746
> >
> > I had to skip n=0 though since that is undefined. Not sure how we
> > handle that since it means no filter. Maybe Jonathan can advise?
>
> Thanks for notes. If I'm reading for datasheet formula right,
>
> k = 2 / (2^n + 1)
>
> So n=0 would be k=1. I did this formula for n=[0-7] and get:
>
> n k
> 0 1.00000
> 1 0.66667
> 2 0.40000
> 3 0.22222
> 4 0.11765
> 5 0.06061
> 6 0.03077
> 7 0.01550
>
> I'm not versed in filter frequency, but would these be the correct
> values to use for the coefficients?
This seems to be what I was looking for:
Got it. For a 1-pole IIR low-pass of the form
y_t = y_{t-1} + k(x_t - y_{t-1}), the –3 dB cutoff is
f_c = \frac{-\ln(1-k)}{2\pi}\,f_s
Using your k=\frac{2}{2^n+1} and a sample rate f_s = 3\ \text{Hz}, the equivalent –3 dB cutoff frequencies are:
n k f_c (Hz) time constant τ (s) = 1/(2π f_c)
0 1.0000000000 ∞ 0.0000
1 0.6666666667 0.5245487288 0.3034
2 0.4000000000 0.2439012692 0.6525
3 0.2222222222 0.1199938006 1.3264
4 0.1176470588 0.0597609987 2.6632
5 0.0606060606 0.0298512716 5.3316
6 0.0307692308 0.0149219903 10.6658
7 0.0155038760 0.0074605397 21.3329
Notes:
• n=0 (k=1) is just “track the input” (no smoothing), so f_c\to\infty.
• If you’re using a different sampling rate, multiply the f_c values above by \frac{f_s}{3}.
--
Ben Collins
https://libjwt.io
https://github.com/benmcollins
--
3EC9 7598 1672 961A 1139 173A 5D5A 57C7 242B 22CF
Download attachment "signature.asc" of type "application/pgp-signature" (834 bytes)
Powered by blists - more mailing lists