discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

2nd order filter in Openscad

JJ
Johan Jonker
Tue, Apr 18, 2023 8:37 PM

Dear Forum

Since long I am fixing some issues in my 3D models.
I created a second order filter function that smooths the angles between
lines of points stored in an array. It works well in Excel and now I
want to implement it in Openscad.

The basic array is created with a function:
 x_rooftop = [ for (n = [0:1:i_in_window_tip]) uncurved_top(n_to_z(n)) ];

Now I want to proces this array through a second order filter.
The problem is that each value n depends on the generated value for n-1.

The function for a first order is Output(n) = Output(n-1) + Constant1 *
(input (n) - Output(n-1))
And for a second order it is Output(n) = Output(n-1) + Constant1 *
(input (n) - Output(n-1)) + Constant2 * (input (n) - Output(n-1))
*(input (n) - Output(n-1))

I cannot figure out how to put that Output(n-1) in a function.

/kind regards //
Johan Jonker/

Dear Forum Since long I am fixing some issues in my 3D models. I created a second order filter function that smooths the angles between lines of points stored in an array. It works well in Excel and now I want to implement it in Openscad. The basic array is created with a function:  x_rooftop = [ for (n = [0:1:i_in_window_tip]) uncurved_top(n_to_z(n)) ]; Now I want to proces this array through a second order filter. The problem is that each value n depends on the generated value for n-1. The function for a first order is Output(n) = Output(n-1) + Constant1 * (input (n) - Output(n-1)) And for a second order it is Output(n) = Output(n-1) + Constant1 * (input (n) - Output(n-1)) + Constant2 * (input (n) - Output(n-1)) *(input (n) - Output(n-1)) I cannot figure out how to put that Output(n-1) in a function. /kind regards // Johan Jonker/
LM
Leonard Martin Struttmann
Tue, Apr 18, 2023 10:21 PM

What do you want to use for the first data point of the filtered result
(where you do not have a previous output)?

On Tue, Apr 18, 2023 at 3:38 PM Johan Jonker info@johanjonker.net wrote:

Dear Forum

Since long I am fixing some issues in my 3D models.
I created a second order filter function that smooths the angles between
lines of points stored in an array. It works well in Excel and now I want
to implement it in Openscad.

The basic array is created with a function:
x_rooftop = [ for (n = [0:1:i_in_window_tip]) uncurved_top(n_to_z(n)) ];

Now I want to proces this array through a second order filter.
The problem is that each value n depends on the generated value for n-1.

The function for a first order is Output(n) = Output(n-1) + Constant1 *
(input (n) - Output(n-1))
And for a second order it is Output(n) = Output(n-1) + Constant1 * (input
(n) - Output(n-1)) + Constant2 * (input (n) - Output(n-1)) *(input (n) -
Output(n-1))

I cannot figure out how to put that Output(n-1) in a function.

*kind regards *

  • Johan Jonker*

OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

What do you want to use for the first data point of the filtered result (where you do not have a previous output)? On Tue, Apr 18, 2023 at 3:38 PM Johan Jonker <info@johanjonker.net> wrote: > Dear Forum > > Since long I am fixing some issues in my 3D models. > I created a second order filter function that smooths the angles between > lines of points stored in an array. It works well in Excel and now I want > to implement it in Openscad. > > > The basic array is created with a function: > x_rooftop = [ for (n = [0:1:i_in_window_tip]) uncurved_top(n_to_z(n)) ]; > > Now I want to proces this array through a second order filter. > The problem is that each value n depends on the generated value for n-1. > > The function for a first order is Output(n) = Output(n-1) + Constant1 * > (input (n) - Output(n-1)) > And for a second order it is Output(n) = Output(n-1) + Constant1 * (input > (n) - Output(n-1)) + Constant2 * (input (n) - Output(n-1)) *(input (n) - > Output(n-1)) > > I cannot figure out how to put that Output(n-1) in a function. > > *kind regards * > * Johan Jonker* > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Wed, Apr 19, 2023 12:14 AM

You need to write it as a recursive function that steps through the array.

Like Leonard, I don't know how you want to "prime" your function, so I
can't give you a specific answer.

But here's a general-purpose function that I just threw together that
will walk through an array, and for each element in the array it will
call a user-provided function.  It will pass  to that function the
element from the array, plus an array that the previous call returned. 
The user-provided function is to take those two arguments and return an
array where the first element is the value to put into the resulting
array, and any subsequent elements are whatever it finds useful.

function map(f, a, prev=undef, i=0) =
    i >= len(a)
    ? []
    : let (tmp = f(a[i], prev))
        concat(tmp[0], map(f, a, tmp, i+1));

sumf = function (v, prev)
    is_undef(prev)
    ? [ v ]
    : [ prev[0] + v ];

echo(map(sumf, [1,2,3,4]));

yields

ECHO: [1, 3, 6, 10]

Note that sumf takes two arguments.  v is the value from the array, and
prev is the array returned by the previous call to sumf, or undef on the
first call.  It returns an array with a single element, the running sum
of the array so far.  As it happens, sumf doesn't need any additional
data, but it could have put more data into the array for use by
subsequent calls.

Or here's a  function that will tell you the difference between
successive pairs of entries in the array.  (The first element of the
resulting array will be undef, because there's one fewer difference than
there are entries in the original.)

diff = function (v, prev)
    is_undef(prev)
    ? [ undef, v ]
    : [ v - prev[1], v ];

echo(map(diff, [4, 2, 76, 12]));

yields

ECHO: [undef, -2, 74, -64]
You need to write it as a recursive function that steps through the array. Like Leonard, I don't know how you want to "prime" your function, so I can't give you a specific answer. But here's a general-purpose function that I just threw together that will walk through an array, and for each element in the array it will call a user-provided function.  It will pass  to that function the element from the array, plus an array that the previous call returned.  The user-provided function is to take those two arguments and return an array where the first element is the value to put into the resulting array, and any subsequent elements are whatever it finds useful. function map(f, a, prev=undef, i=0) = i >= len(a) ? [] : let (tmp = f(a[i], prev)) concat(tmp[0], map(f, a, tmp, i+1)); sumf = function (v, prev) is_undef(prev) ? [ v ] : [ prev[0] + v ]; echo(map(sumf, [1,2,3,4])); yields ECHO: [1, 3, 6, 10] Note that sumf takes two arguments.  v is the value from the array, and prev is the array returned by the previous call to sumf, or undef on the first call.  It returns an array with a single element, the running sum of the array so far.  As it happens, sumf doesn't need any additional data, but it could have put more data into the array for use by subsequent calls. Or here's a  function that will tell you the difference between successive pairs of entries in the array.  (The first element of the resulting array will be undef, because there's one fewer difference than there are entries in the original.) diff = function (v, prev) is_undef(prev) ? [ undef, v ] : [ v - prev[1], v ]; echo(map(diff, [4, 2, 76, 12])); yields ECHO: [undef, -2, 74, -64]
JB
Jordan Brown
Wed, Apr 19, 2023 12:16 AM

On 4/18/2023 5:14 PM, Jordan Brown wrote:

 function map(f, a, prev=undef, i=0) =

Note that, if you like, the caller can supply an initial value for
"prev" and then the function that you supply doesn't have to handle "undef".

On 4/18/2023 5:14 PM, Jordan Brown wrote: > > function map(f, a, prev=undef, i=0) = > Note that, if you like, the caller can supply an initial value for "prev" and then the function that you supply doesn't have to handle "undef".
RW
Rob Ward
Wed, Apr 19, 2023 7:56 AM

Why not just allow X=X+1
An accumulator/index increment?
Cheers, RobW

On 19 April 2023 10:16:40 am AEST, Jordan Brown openscad@jordan.maileater.net wrote:

On 4/18/2023 5:14 PM, Jordan Brown wrote:

 function map(f, a, prev=undef, i=0) =

Note that, if you like, the caller can supply an initial value for
"prev" and then the function that you supply doesn't have to handle "undef".

Why not just allow X=X+1 An accumulator/index increment? Cheers, RobW On 19 April 2023 10:16:40 am AEST, Jordan Brown <openscad@jordan.maileater.net> wrote: >On 4/18/2023 5:14 PM, Jordan Brown wrote: >> >> function map(f, a, prev=undef, i=0) = >> > >Note that, if you like, the caller can supply an initial value for >"prev" and then the function that you supply doesn't have to handle "undef".
RD
Revar Desmera
Wed, Apr 19, 2023 8:56 AM

That would violate the sacred precepts of the church of Functional Programming, in which variables aren’t variable.

-Revar

On Apr 19, 2023, at 12:56 AM, Rob Ward rl.ward@bigpond.com wrote:


Why not just allow X=X+1
An accumulator/index increment?
Cheers, RobW

On 19 April 2023 10:16:40 am AEST, Jordan Brown openscad@jordan.maileater.net wrote:
On 4/18/2023 5:14 PM, Jordan Brown wrote:

function map(f, a, prev=undef, i=0) =

Note that, if you like, the caller can supply an initial value for "prev" and then the function that you supply doesn't have to handle "undef".


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

That would violate the sacred precepts of the church of Functional Programming, in which variables aren’t variable. -Revar > On Apr 19, 2023, at 12:56 AM, Rob Ward <rl.ward@bigpond.com> wrote: > >  > Why not just allow X=X+1 > An accumulator/index increment? > Cheers, RobW > > >> On 19 April 2023 10:16:40 am AEST, Jordan Brown <openscad@jordan.maileater.net> wrote: >> On 4/18/2023 5:14 PM, Jordan Brown wrote: >>> function map(f, a, prev=undef, i=0) = >> >> Note that, if you like, the caller can supply an initial value for "prev" and then the function that you supply doesn't have to handle "undef". >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
RW
Rob Ward
Wed, Apr 19, 2023 9:38 AM

So it's the church v progress??
Cheers, RobW

On 19 April 2023 6:56:49 pm AEST, Revar Desmera revarbat@gmail.com wrote:

That would violate the sacred precepts of the church of Functional Programming, in which variables aren’t variable.

-Revar

On Apr 19, 2023, at 12:56 AM, Rob Ward rl.ward@bigpond.com wrote:


Why not just allow X=X+1
An accumulator/index increment?
Cheers, RobW

On 19 April 2023 10:16:40 am AEST, Jordan Brown openscad@jordan.maileater.net wrote:
On 4/18/2023 5:14 PM, Jordan Brown wrote:

function map(f, a, prev=undef, i=0) =

Note that, if you like, the caller can supply an initial value for "prev" and then the function that you supply doesn't have to handle "undef".


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

So it's the church v progress?? Cheers, RobW On 19 April 2023 6:56:49 pm AEST, Revar Desmera <revarbat@gmail.com> wrote: >That would violate the sacred precepts of the church of Functional Programming, in which variables aren’t variable. > >-Revar > > >> On Apr 19, 2023, at 12:56 AM, Rob Ward <rl.ward@bigpond.com> wrote: >> >>  >> Why not just allow X=X+1 >> An accumulator/index increment? >> Cheers, RobW >> >> >>> On 19 April 2023 10:16:40 am AEST, Jordan Brown <openscad@jordan.maileater.net> wrote: >>> On 4/18/2023 5:14 PM, Jordan Brown wrote: >>>> function map(f, a, prev=undef, i=0) = >>> >>> Note that, if you like, the caller can supply an initial value for "prev" and then the function that you supply doesn't have to handle "undef". >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org
TP
Torsten Paul
Wed, Apr 19, 2023 1:09 PM

On 19.04.23 11:38, Rob Ward wrote:

So it's the church v progress??

No, nothing of that nonsense.

On 19.04.23 11:38, Rob Ward wrote: > So it's the church v progress?? No, nothing of that nonsense.
CA
Carsten Arnholm
Wed, Apr 19, 2023 1:25 PM

On 19.04.2023 11:38, Rob Ward wrote:

So it's the church v progress??
Cheers, RobW

No, just the church

Cheers

On 19.04.2023 11:38, Rob Ward wrote: > So it's the church v progress?? > Cheers, RobW No, just the church Cheers
TP
Torsten Paul
Wed, Apr 19, 2023 1:27 PM

On 19.04.23 15:25, Carsten Arnholm wrote:

No, just the church

https://xkcd.com/1357/

ciao,
Torsten.

On 19.04.23 15:25, Carsten Arnholm wrote: > No, just the church https://xkcd.com/1357/ ciao, Torsten.