Mar 27 2023 08:31 PM
Mar 27 2023 08:31 PM
Hey guys, hope you doing great!
I have been playing with some data at work and wondered what would happen if i made some kernel to convolve with my main vector to smooth it with a moving-average like operation as shown in this awesome video by Grant at 3Blue1Brown
I started searching the forum for some solution to this and came by some made by @Peter Bartholomew while discussing about Fourier Transforms, but i found the solution to be too complicated for my use as it needed recursion and a lot of defined names, so i went for my own solution which involved offsetting the vector while the kernel stays which is the opposite of what is shown in the video.
The first thing i thought was that i needed a sequence that could be used to offset the vector so i could use this offsetted vector to SUMPRODUCT with the kernel and since the size of the convolution must be:
vector + kernel - 1 by definition
i made a formula to make this sequence:
=SEQUENCE( ROWS(vector) + ROWS(kernel) - 1; 1; -(ROWS(kernel) - 1); 1 )
*im using a vector of 100 rows and a kernel of 4 rows*
This sequence then starts at -3 and goes to 99 one by one thus obeying convolution size definition, but why do i need it in the first place? so i can generate an array of height 4 (kernel size) which offsets the vector taking the sequence as its row offset argument:
where A4 is the first result of the sequence formula, now i can drag down the formula for all numbers in the sequence and it will generate the output i need to transpose again and then sumproduct:
where F4# is the output of the transpose offset formula above and then i drag down for each result and voila the result of the convolution is done (assuming the kernel was already flipped)
Now to make it dynamic i first made a formula that takes the vector and the kernel and automatically generates the sequence:
=LAMBDA(vector1; kernel1; LET( vector_r; ROWS(vector1); kernel_r; ROWS(kernel1); convolution_r; ((vector_r) + (kernel_r) - 1); offset_sequence; SEQUENCE(convolution_r;1;-(kernel_r - 1)); offset_sequence ) )(vector; kernel)
And a second formula to take the results:
=MAP( M4#; LAMBDA(seq; SUMPRODUCT( OFFSET( vector; seq; 0; ROWS(kernel) ); kernel ) ) )
where M4# is the result of the first dynamic formula
This does the trick but has space for improvement, first of all the vector needs space to be pad with zeroes which should be handled inside the formula but i couldnt do it.
Second, i should be able to combine the two formulas at least in my head it should be possible i tried to do it but dindnt work... ill be leaving my test file to show it more properly what i have done
The goal here is to learn how to properly pad arrays for this kind of offsetting and how to properly combine these formulas if possible, and, if this is not proper at all i would like to know...
Also note: i use semicolon instead of commas at the formula because i live in Brazil and the formula syntax here works like that for some reason...
Mar 28 2023 02:55 PM
Mar 28 2023 02:55 PM
The video to which you provided the link is very good. I had watched others by the same presenter.
"too complicated for my use as it needed recursion"
I try to avoid recursion where possible but it can be valuable, especially when it is not possible to predict in advance how many steps will be needed. I have a non-recursive version of the convolution but it is no simpler.
"and a lot of defined names", I have avoided direct cell referencing for the past 8 years, preferring instead to use names both for identifying ranges and to define formulas for evaluation. There are plusses, I no longer need to use relative references, so a formula will evaluate to the same result wherever I place it, and I no longer need to worry about following precedents to help decode a formula.
"so I went for my own solution which involved offsetting the vector while the kernel stays which is the opposite of what is shown in the video" One learns more by going one's own way! The convolution has an inherent symmetry allowing the role of the value array and the kernel to be reversed.
I did try using INDEX in place of OFFSET in case the input range was being dereferenced to produce an array rather than a reference but without success. Sorry about that.
An advantage of Lambda functions is that they are not as difficult to deploy as they are to develop! The first file attached has links to the theoretical basis, which is interesting but something of a nightmare nonetheless. I also applied the Convolve function to a ModelOff problem recently, as presented by @Diarmuid Early . The links to his problem solution show how traditional techniques may be used, especially if you are a World-class Excel modeller!