in

Fourier Remodel for Time Sequence: Quick Convolution Defined with numpy | by Yoann Mocquin | Jul, 2023


Implementation from scratch vs numpy

The Fourier rework algorithm is taken into account one of many best discoveries in all of arithmetic. French mathematician Jean-Baptiste Joseph Fourier laid the muse for harmonic evaluation in his e-book “Théorie analytique de la chaleur” in 1822. Right this moment, the Fourier rework and all its variants kind the premise of our trendy world, powering applied sciences like compression, communication, picture processing.

Engraved portrait of French mathematician Jean Baptiste Joseph Fourier (1768–1830), early nineteenth century. [souce: wikipedia, image from public domain]

This excellent framework additionally supplies nice instruments for analysing time-series… and that’s why we’re right here!

This submit is a part of a collection on the Fourier rework. Right this moment we are going to speak about convolution and the way the Fourier rework supplies the quickest method you are able to do it.

All figures and equations are made by the writer.

Let’s begin with fundamental definitions. The discrete Fourier rework for a discrete time sequence x of N parts is :

Discrete Fourier Remodel (DFT) definition. Different definitions exist, you simply want to decide on one and keep on with it (made by writer)

the place okay denotes the k-th frequency of the spectrum of x. Observe that some writer add a scaling issue of 1/N to that definition, however is just not of nice significance for this submit — all in all it’s only a matter of definition and sticking it to it.

The inverse Fourier rework is then (given the definition for the ahead Fourier rework):

Inverse Discrete Fourier Remodel, based mostly on the ahead definition talked about above (made by writer).

That being mentioned, some of the vital theorem on Fourier rework is that convolution in a single area is equal to multiplication within the different. In different phrases, the Fourier rework of a product is the convolution of the respective Fourier spectra, and the Fourier rework of a convolution is the product of the respective Fourier spectra.

Multiplication within the time-domain corresponds to round convolution in Fourier area (made by writer).

and

Round convolution in time area corresponds to multiplication in Fourier area (made by writer).

the place the dot represents the usual product (multiplication) and the circled star represents round convolution.

Two vital notes:

  • Periodic sign : Fourier evaluation framework take into account that the indicators we deal with are periodic. In different phrases, they repeat from minus infinity to infinity. Nonetheless it isn’t all the time sensible to deal with such indicators with a finite reminiscence pc, so we solely “play” with one interval, as we are going to see hereafter.
  • Round convolution : The convolution theorem states that multiplication is equal to circulation convolution, which is a bit completely different from linear convolution, which we’re extra accustomed to. As we are going to see, it isn’t that completely different, and never that difficult both.

If you happen to’re accustomed to linear convolution, typically merely known as ‘convolution’, you gained’t be confused by round convolution. Mainly, round convolution is simply the best way to convolve periodic indicators. As you’ll be able to guess, linear convolution solely is sensible for finite size indicators, that don’t span from minus infinity to infinity. In our case, within the context of Fourier evaluation, our indicators are periodic subsequently don’t fulfill this situation. We will’t speak about (linear) convolution.

But we nonetheless can intuit a linear-convolution-like operation on our periodic indicators : simply convolve the periodic sign on a one-period size. That’s what round convolution does : it convolves 2 periodic indicators of the identical size alongside a one-period span.

To additional persuade your self of the distinction, examine each formulation for discrete linear convolution and discrete round convolution :

Equation for linear convolution : more often than not in sign processing, with use this method, by padding with zeros (made by writer).
Round convolution : that is the convolution used when coping with periodic indicators, as in Fourier evaluation (made by writer).

Discover the variations :

bounds : linear convolution makes use of samples from minus-infinity to plus infinity — as said beforehand, on this context x and y have finite vitality the sum is sensible. For the round convolution, we solely must what occurred in a interval span, so the sum solely spans one interval

round indexes : within the round convolution, we “wrap” the index of y utilizing a modulo operation with a size of N. That’s only a method to make sure that y is taken into account periodic with a interval of N : once we need to know the worth of y at place okay, we simply use the worth of y at place kpercentN — since y is N periodic, we get the correct worth. Once more, that is only a mathematical strategy to deal with periodic infinite-length pattern sequences.

Numpy supplies nice instruments for finite size indicators: and that’s a excellent news as a result of as we simply noticed, our infinite-length periodic sign will be represented with only a interval.

Let’s create a easy class to characterize such indicators. We add a technique to shortly plot the array, in addition to a further interval earlier than and after the “base” array, to remember that we’re coping with a periodic sequence.

Let’s take a look at two examples : first with a sampled sinus sequence, then with a linear sequence. Each are thought of to be N-periodic (N=10 on this case).

2 examples of PeriodicArray : the “base” interval is plotted from 0 to N in darkish blue, whereas 2 different intervals are added earlier than and after to characterize the truth that we’re coping with periodic sequences (made by writer).

Let’s now implement the round convolution equation seen above. Utilizing indexing and the modulo operator, it’s fairly simple:

The round convolution between the above 2 periodic sequences (made by writer).

That’s nice, we are able to now see what the round convolution between two indicators seems to be like. Placing the whole lot in a single determine :

Left : first periodic array. Center : second periodic array. Proper : round convolution of the two periodic arrays, which is also a periodic array (made by writer).

Now this resolution works very well, but it surely has a significant flaw : it’s sluggish. As you’ll be able to see, we now have to undergo 2 nested loops to compute the consequence : one for every place within the consequence array, and one to compute the consequence at that place : we are saying that the algorithm is O(N²), as N will increase the variety of operations will enhance because the sq. of N.

For small arrays like these within the instance, it isn’t an issue, however as your array will develop it’ll turn out to be a significant drawback.

Moreover, loops on numerical information is more often than not thought of a foul follow in python. There should be a greater method…

And that’s the place the Fourier rework and the convolution theorem come into play. Due to the best way the discrete Fourier rework is carried out, in a really quick and optimized method utilizing the Quick Fourier Remodel (FFT), the operation is **very** quick (we are saying the FFT is O(N log N), which is method higher than O(N²)).

Utilizing the convolution theorem, we are able to use the actual fact the product of the DFT of two sequences, when remodeled again into the time-domain utilizing the inverse DFT, we get the convolution of the enter time sequences. In different phrases, we now have :

Round convolution between x and y utilizing direct and inverse Fourier rework (made by writer).

the place DFT represents the discrete Fourier rework, and IDFT the inverse operation.

We will then implement this algorithm to compute the convolution of x and y utilizing numpy very simply :

To complete, let’s confirm that each approaches yield the identical outcomes, and examine the time it take python to compute the round convolution utilizing each methods:

Comparability of each approaches to compute the round convolution between 2 periodic sequences : “sluggish method” is the easy algebraic utilizing loops and additions in blue, which is superimposed with the “Fourier method” in orange. Each strategies give precisely equal outcomes (all the way down to numerical precision) (made by writer).

That’s an ideal match! Each are rigorously equal in time period of numerical values.

Now for the time comparability:

And the outcomes are:

  • for N=10 samples, the DFT is quicker by an element of 6
  • for N=1000 samples, the DFT is quicker an element of about 10000

That’s big! Take into account now what it could possibly brings you while you analyze your time-series with hundreds and hundreds of samples!

Now we have seen on this submit that the Fourier rework is highly effective instrument, particularly because of the convolution theorem that permits us to compute convolution in a really environment friendly method. We’ve seen that linear and round are usually not precisely the identical operation however are each based mostly on a convolution.

Subscribe to get future posts about Fourier rework straight onto your feed!

Additionally, try my different submit and when you like all of them, please subscribe it helps me rather a lot to succeed in my purpose of 100 subscribers:

300-Times Faster Resolution of Finite-Difference Method Using NumPy | by Yoann Mocquin | Towards Data Science (medium.com)

PCA/LDA/ICA : a components analysis algorithms comparison | by Yoann Mocquin | Towards Data Science (medium.com)

Wrapping numpy’s array. The container approach. | by Yoann Mocquin | Towards Data Science (medium.com)

Deep Dive into Seaborn: Color Palettes | by Yoann Mocquin | Analytics Vidhya | Medium


Predict automobile fleet failure likelihood utilizing Amazon SageMaker Jumpstart

Analyzing Flights within the U.S. with AWS and Energy BI | by Aashish Nair | Jul, 2023