r/Python • • Oct 14 '19

When Python Meets Fourier Transform

2.3k Upvotes

122 comments sorted by

29

u/Todask Oct 14 '19

Is this code published?

29

u/vaiv101 Oct 14 '19

Will do it soon

8

u/Todask Oct 14 '19

I'd love to see it! Please post here

4

u/vaiv101 Oct 14 '19

Sure

14

u/[deleted] Oct 14 '19

I love how the remindme bot has a line that clearly states to just click the fucking link to reduce spam and they will be reminded anyways and yet five people commented !rEMiNdMe OnE wEEk

6

u/kickthebug Oct 14 '19

!RemindMe one week

1

u/RemindMeBot Oct 14 '19 edited Oct 20 '19

I will be messaging you on 2019-10-21 17:06:16 UTC to remind you of this link

65 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.

There is currently another bot called u/kzreminderbot that is duplicating the functionality of this bot. Since it replies to the same RemindMe! trigger phrase, you may receive a second message from it with the same reminder. If this is annoying to you, please click this link to send feedback to that bot author and ask him to use a different trigger.


InfoCustomYour RemindersFeedback

-5

u/1Mario_55 Oct 14 '19

!RemindMe one week

1

u/kzreminderbot Oct 21 '19

Ding dong! ⏰ Here's your reminder.

/r/Python: When_python_meets_fourier_transform

You requested this reminder 1 week ago on 2019-10-14 19:51:06Z

If reminder notification has helped you, let us know.

Reminder Actions: Get Details | Delete


Bot Information | Create Reminder | Your Reminders | Give Feedback

0

u/kzreminderbot Oct 14 '19

Good day, 1Mario_55 🐣! I will notify you in 7 days on 2019-10-21 19:51:06Z to remind you of:

/r/Python: when_python_meets_fourier_transform

CLICK THIS LINK to send a PM to also be reminded and to reduce spam. Thread has 8 reminders.

1Mario_55 can Delete Comment | Delete Reminder | Get Details | Update Time | Update Message


InfoCreateYour RemindersFeedback

-3

u/dimkal Oct 15 '19

!RemindMe one week

3

u/DatBoi_BP Oct 15 '19

Copy that, dimkal 🤗! Just click the link above like everyone else for fuck sake

-3

u/mrshibx Oct 15 '19

!RemindMe one week

-5

u/nielsrolf Oct 14 '19

!RemindMe one week

1

u/kzreminderbot Oct 21 '19

Ding dong! ⏰ Here's your reminder.

/r/Python: When_python_meets_fourier_transform

!RemindMe one week

You requested this reminder 1 week ago on 2019-10-14 19:10:10Z

If reminder notification has helped you, let us know.

Reminder Actions: Get Details | Delete


Bot Information | Create Reminder | Your Reminders | Give Feedback

0

u/kzreminderbot Oct 14 '19 edited Oct 15 '19

Copy that, nielsrolf 🤗! Your reminder is in 7 days on 2019-10-21 19:10:10Z :

/r/Python: when_python_meets_fourier_transform

!RemindMe one week

1 OTHER CLICKED THIS LINK to also be reminded and to reduce spam. Thread has 13 total reminders and reached max of 4 confirmation comments. Additional confirmations are sent by PM.

nielsrolf can Delete Comment | Delete Reminder | Get Details | Update Time | Update Message


Bot InformationCreate ReminderYour RemindersReport Issue

-6

u/nihi_ Oct 14 '19

!RemindMe one week

1

u/kzreminderbot Oct 14 '19 edited Oct 14 '19

Copy, nihi_ 🐣! I will notify you in 7 days on 2019-10-21 17:16:54Z to remind you of:

/r/Python: when_python_meets_fourier_transform

1 OTHER CLICKED THIS LINK to send a PM to also be reminded and to reduce spam. Thread has 3 reminders.

nihi_ can Delete Comment | Delete Reminder | Get Details | Update Time | Update Message


InfoCreateYour RemindersFeedback

1

u/kzreminderbot Oct 21 '19

Ding dong! ⏰ Here's your reminder.

/r/Python: When_python_meets_fourier_transform

You requested this reminder 1 week ago on 2019-10-14 17:16:54Z

If reminder notification has helped you, let us know.

Reminder Actions: Get Details | Delete


Bot Information | Create Reminder | Your Reminders | Give Feedback

0

u/[deleted] Oct 14 '19

[deleted]

1

u/kzreminderbot Oct 14 '19

Hi, hlodynn_ 🐣! I will notify you in 7 days on 2019-10-21 18:25:08Z to remind you of:

/r/Python: when_python_meets_fourier_transform

CLICK THIS LINK to send a PM to also be reminded and to reduce spam. Thread has 6 reminders.

hlodynn_ can Delete Comment | Delete Reminder | Get Details | Update Time | Update Message


InfoCreateYour RemindersFeedback

1

u/humble_fool Oct 15 '19

!RemindMe 1 week

1

u/kzreminderbot Oct 21 '19

Ding dong! ⏰ Here's your reminder.

/r/Python: When_python_meets_fourier_transform

You requested this reminder 1 week ago on 2019-10-14 18:25:08Z

If reminder notification has helped you, let us know.

Reminder Actions: Get Details | Delete


Bot Information | Create Reminder | Your Reminders | Give Feedback

0

u/[deleted] Oct 14 '19

[deleted]

1

u/kzreminderbot Oct 14 '19 edited Oct 15 '19

Roger, Hariharan__SJ 🐣! Your reminder is in 7 days on 2019-10-21 18:12:06Z :

/r/Python: when_python_meets_fourier_transform

2 OTHERS CLICKED THIS LINK to also be reminded and to reduce spam. Thread has 16 total reminders and reached max of 4 confirmation comments. Additional confirmations are sent by PM.

Hariharan__SJ can Delete Comment | Delete Reminder | Get Details | Update Time | Update Message


Bot InformationCreate ReminderYour RemindersReport Issue

1

u/kzreminderbot Oct 21 '19

Ding dong! ⏰ Here's your reminder.

/r/Python: When_python_meets_fourier_transform

You requested this reminder 1 week ago on 2019-10-14 18:12:06Z

If reminder notification has helped you, let us know.

Reminder Actions: Get Details | Delete


Bot Information | Create Reminder | Your Reminders | Give Feedback

-1

u/zeta-1 Oct 15 '19

!RemindMe 2 days

4

u/danielhoherd Oct 14 '19

Here's a great interactive article about this, with links to the code (not python): http://www.jezzamon.com/fourier

8

u/a5myth Oct 14 '19

You know how we have two main types of graphic programs, raster (pixel) and vector (lines with addresses and directions, etc), would it be possible to create a graphics program that uses fourier series calculations instead?

3

u/vaiv101 Oct 14 '19

hmm .. can be tried, but I can not say for sure if it is possible or already done by someone

3

u/5Beans6 Oct 15 '19

Probably would be really slow and impractical for anything useful

2

u/justingolden21 Oct 15 '19

Definitely. Vectors are waaaaay less math. There's no point other than educational in representing data like this. Don't get me wrong Fourier transforms are useful for lots of real world stuff, just not this

6

u/augle Oct 14 '19

Pretty cool. Where can I find more info on this, or can you briefly explain how that was done?

13

u/vaiv101 Oct 14 '19

For now, you can have a look at this video for reference, i am going to upload the coad on github soon..

https://youtu.be/r6sGWTCMz2k

6

u/Ketta Oct 14 '19

Your typo made me think of a coding toad and that is awesome.

1

u/FauxReal Oct 14 '19

You just designed the next O'Reilly book cover, if it doesn't already exist.

4

u/[deleted] Oct 14 '19

is it possible to inverse the circle order? Big one spinning around?

10

u/glacierre2 Oct 14 '19

Yes it is, but instead of a pleasant "clock-like"video you would get a shaky bunch of circles.

0

u/[deleted] Oct 14 '19 edited Oct 14 '19

Thats not how the fourier series works, so no (afaik) edit:im wrong

10

u/wewbull Oct 14 '19

Fourier series are a sum of frequencies. Addition is transitive, so yes /u/32BitLongSucks, the order could be anything. It's just that having the highest frequency at the centre would make the whole thing spin very fast.

1

u/[deleted] Oct 14 '19

so the order isn't amplitude it's frequency, makes sense! Mixing the elements could create some fun effects, but putting the highest frequency first seems like guaranteed chaos - could be fun, probably isn't.

-6

u/vaiv101 Oct 14 '19

Nope, this is not possible, for a better explanation of this, you can read about Fourier transformation

6

u/[deleted] Oct 14 '19

i figured the fourier series is just a sum so the order of the elements shouldn't be important

-5

u/vaiv101 Oct 14 '19

I believe you have some doubts, have a look at this video https://youtu.be/r6sGWTCMz2k

4

u/Meowkit Oct 14 '19

I believe you don’t understand what you are linking to.

Summation is commutative, adding the circles can be done in any order, thus the circles could be displayed/linked in any order.

429

u/w8cycle Oct 14 '19

Looks like a "furrier" transformation 😉

14

u/Mooks79 Oct 15 '19

I had to paws to understand that.

5

u/conventionistG Oct 15 '19

Cat got your tongue?

60

u/Cosmic_Sands Oct 14 '19

owo

0

u/[deleted] Oct 14 '19

[deleted]

5

u/Jwestie15 Oct 14 '19

What's this

5

u/nspectre Oct 15 '19

[deleted]

58

u/nielsrolf Oct 14 '19

You need help understanding the complex Fourier Transform? Ask OP. Need some support with Python? OP knows. Improve your data visualization skills? OP is your guy. Take a screen video? Hm that's hard

0

u/[deleted] Oct 14 '19

[deleted]

27

u/AgArgento Oct 14 '19

Original poster. He means you.

12

u/fatbob42 Oct 14 '19

Decoding internet forum slang? Nope!

Luckily, we have people for that :)

74

u/__unavailable__ Oct 14 '19

That's a surprisingly intuitive way of depicting fourier series.

9

u/Helicon_Amateur Oct 14 '19

Can you elaborate?

89

u/[deleted] Oct 14 '19

"Hey, what's a fourier transform?"

Option 1: Present a bunch of equations

Option 2: The Fourier transform (FT) decomposes a function of time (a signal) into its constituent frequencies. This is similar to the way a musical chord can be expressed in terms of the volumes and frequencies of its constituent notes. The term Fourier transform refers to both the frequency domain representation and the mathematical operation that associates the frequency domain representation to a function of time. The Fourier transform of a function of time is itself a complex-valued function of frequency, whose magnitude (modulus) represents the amount of that frequency present in the original function, and whose argument is the phase offset of the basic sinusoid in that frequency. The Fourier transform is not limited to functions of time, but the domain of the original function is commonly referred to as the time domain. There is also an inverse Fourier transform that mathematically synthesizes the original function from its frequency domain representation.

Option 3: Here's a spirograph drawing a dog

3

u/PM_ME_VINTAGE_30S Oct 14 '19

I'll take option 2 for $500. No but seriously, I'm saving this. I'm in diff EQ and I think I'll be learning this formally in about a month. 👍Ofc I'm an electrical engineering major and I intend to specialize in DSP so Fourier transforms will be life.

0

u/[deleted] Oct 14 '19

[deleted]

24

u/ForgottenWatchtower Oct 14 '19

Nothing is stopping you. Plenty of info freely available on the web. 3b1b is a great YT channel to refresh/learn many mathematical fundamentals, including Fourier transforms.

8

u/waxen_earbuds Oct 14 '19

If you want to understand it at the level of the person you are replying to, you are better off studying applied math.

16

u/[deleted] Oct 14 '19

while true, i just copied wikipedia's intro paragraph

2

u/[deleted] Oct 14 '19

You should look into DSP. It's not just an engineering problem - Carl Friedrich Gauss apparently made bank on the stock market using signal processing techniques.

1

u/[deleted] Oct 15 '19

It's called technical analysis. There's a python binding for the TA-lib. But it's a good way to go broke because everyone else is also doing TA on stocks and they'll beat you to it.

6

u/potatochemist Oct 14 '19

Essentially, if you add up the right combination of sine waves with different heights and speeds, you can recreate any function/graph/picture.

3

u/Helicon_Amateur Oct 14 '19

Okay. Now I get it and feel really stupid for not thinking of it that way. I understand what an FFT does but didn't relate it to this picture.

So each frame of this drawing should then consist of a peice of a signal, say there is this really long signal in the time domain - then stepping over a finite chunk of that signal would produce this graph, is that correct?

4

u/potatochemist Oct 14 '19

The "signal" is the shape of the dog. However instead of volume over time (like a microphone), it's x-y pairs over time where each pair is a point somewhere on the dog.

They then take the FFT of this to get the size and speed (magnitude and frequency) of the sin waves shown by the circles.

Here's a gif of the same thing with a time signal (like volume).

Here's a really good video if you want to get a bit deeper.

3

u/FauxReal Oct 14 '19

Funny lookin' muzzle on that dog of yours.

5

u/AnythingApplied Oct 14 '19

One thing to be careful of is that this fourier transform isn't just based on the shape, but more specifically the shape and speed of drawing. If the pencil is drawing at a constant distance per unit time vs constant angle from the origin vs any other speed arrangement, you'll get a different fourier series.

0

u/[deleted] Oct 14 '19

[deleted]

4

u/[deleted] Oct 14 '19

Could you prove that intuitively for a layman?

12

u/waxen_earbuds Oct 15 '19 edited Oct 15 '19

Consider the following approximate, ad hoc statement of Fourier’s theorem: any periodic signal may be represented as an infinite sum of simple harmonics. Well, okay, that’s great and all, but how does this apply here? The key is that op’s dog here is a closed path through the complex plane. In other words, given some amount of time that it would take to trace that path, given some parametrization, you have a notion of fundamental frequency. The component of the “dog signal” (DS) that is captured by that fundamental is encoded in magnitude (the radius) and phase (the starting angle) of the first line segment. As we discussed, the speed is encoded by the frequency that you define. This is a symmetry of a path representation of a complex signal, so you could think of there as being infinitely many possible “fundamental frequencies”. That center line segment traces a circle, which you could think of as a very crude approximation of the DS. Recalling the visual intuition for 2D vector/complex addition, if we added a complex number to this first fundamental signal, it would effectively extend the path in whatever direction our second segment was pointing by whatever its magnitude is. The second such segment would proceed at twice the speed of the fundamental, third thrice, ad infinitum, with each step providing a bit more clarity. We do this in harmonics of the fundamental frequency, since only integer multiples of the fundamental can be periodic with respect to the base signal. But thanks to Mr. Fourier, we know that is all we need. Just infinitely many.

In mathematical terms, you can think of the first circle as being some horizontal component Acos(wt+φ) and a vertical component Asin(wt+φ) for some amplitude A, phase offset φ, and fundamental frequency w=2pi/T, where T is the amount of time it takes to traverse the path. Should you choose to represent this as a complex number, using Euler’s identity, we have Aexp(i(wt + φ)). Since we have established that the amplitude and phase are unique for each constituent integer multiple of the fundamental frequency, we can represent this in general for the k’th such frequency as A_k exp(i(kwt + φ_k)). To sum this all the way to the number of circles at our disposal, we can represent our complex signal z parametrized by time as

$ z(t) \approx \sum_{k=1}N A_k e{i(k\omega t + \phi_k)} $

This is the statement of the discrete, complex Fourier series for a zero-offset path. There is much more to be said here, but I will refer you to Wikipedia or 3b1b for further elaboration.

Hope that helps!

3

u/DatBoi_BP Oct 15 '19

They more generally mean Fourier Transform. Anyway, the x–axis is real, y–axis is imaginary. Fourier Transform takes the "signal" (in this case, the set of points discretely outlining this animal) and essentially decomposes it into a bunch of amplitudes and phase angles (complex numbers' magnitudes and arguments, respectively) for a set of frequencies that depends on the sampling rate. These directly correspond to the circles we see—radius = amplitude; initial direction of line = phase angle; rate of change of line direction = frequency. Center the next circle on the end of the line of the current circle, and once you get to the last circle line, plot that point as it changes over time. That recreates the image

24

u/qevlarr Oct 14 '19

Someone watched 3blue1brown

1

u/vaiv101 Oct 15 '19

The code for the above video is here, https://github.com/VaibhavSingh24/python-fourier-transform-images

If you want a proper explanation, I can make a video over it. Please tell me if I should

1

u/frany_RL Oct 14 '19

When will you upload the code? Today?? Xd

1

u/vaiv101 Oct 15 '19

Done, check it out

1

u/kekomat11 Oct 15 '19

Could you publish the code on GitHub?

1

u/vaiv101 Oct 15 '19

Yup, done already

4

u/log_2 Oct 14 '19

This doesn't look right. There are moments when the biggest circle speeds up. All circles should be rotating at a constant speed, because changing speed changes frequency and each circle is meant to represent one constant frequency.

2

u/kkingsbe Oct 15 '19

I'm pretty sure it just runs faster with certain calculations, which means that the "frame rate" is slowing, but they are all moving at the same speed relative to each other. I ran this same issue with a rocket simulation that I am writing in C++

1

u/Thisnametakenffs Oct 14 '19

They are all spinning at the same constant rate. It only appears to move fast because in order to draw a long line many circles must spin together in the same direction.

2

u/DenormalHuman Oct 15 '19

But what does it sound like?

1

u/[deleted] Oct 14 '19

advanced dog drawing

1

u/alpha_vantage Oct 14 '19

In the future all drawings will be done like this

1

u/Acid_Monster Oct 14 '19

Was really expecting a dickbutt here, I’m disappointed

1

u/Dubnos willToLive = mySistersIQ(0) Oct 14 '19

Those are a lot of good bois

1

u/redimusu76 Oct 14 '19

This is so cooll!!!

1

u/kn0xchad Oct 15 '19

!RemindMe 1 week

1

u/timetostepoutside Oct 15 '19

!RemindMe 1 week

1

u/b4xt3r Oct 15 '19

What the hell am I watching that looks awesome......

1

u/guinader Oct 15 '19

Dogecoin confirmed

1

u/Crossfire234 Oct 15 '19

When you can code a Fourier Transform animation but can't render a gif.

1

u/[deleted] Oct 15 '19

Dope

1

u/Ulriklm Oct 15 '19

Wow this is awesome!! How do I learn python from the bottom, can anyone recommend a book. I've programmed a bit in Java.

1

u/[deleted] Oct 15 '19 edited Oct 28 '19

RuglBHp8qWfOyskWknE4D7Bf9jhcHkcB

0

u/lostshot12 Oct 14 '19

someone watched coding train

0

u/Atsushi_7 Oct 14 '19

!RemindMe one week

0

u/LtheUandE42 Oct 14 '19

!RemindMe one week

0

u/real-elon Oct 14 '19

!RemindMe 1 month

0

u/[deleted] Oct 14 '19

[deleted]

42

u/MisterBlggs gitlab.com/MisterBiggs Oct 14 '19

Or even Google "how to export matplotlib as video". In the end the video is still easy enough to understand so I guess the post served it's purpose🤷‍♂️

50

u/GetOnMyLevelL Oct 14 '19

Yes or taking a picture instead of printscreen

49

u/locoluis Oct 14 '19

Or making a Python program that outputs this as a video…

42

u/raybrignsx Oct 14 '19

Or beaming this animation straight into my mind.

78

u/vectorpropio Oct 14 '19

Oh yes, the ol'

from future import telepathy

9

u/MaliciousHH Oct 14 '19

future is a handy R package

0

u/billsil Oct 15 '19

GIFs are better. They're smoother and the quality is quite good when you have a limited color palate, which seems to be anything that's not real life.

4

u/ivosaurus pip'ing it up Oct 15 '19

A proper video codec is infinitely better than any gif.

1

u/billsil Oct 15 '19

Depends what you’re trying to do with it. If you’re putting it in a PowerPoint, I’ll take the GIF. For YouTube, yes.

1

u/ivosaurus pip'ing it up Oct 21 '19

If you're using PowerPoint from a version less than a decade old, it can handle mp4 natively.

1

u/billsil Oct 21 '19

It’s more trying to get an acceptable quality and making it loop indefinitely that I like. There might be a setting for mp4. I also don’t want to hit the play button during a presentation.

Additionally, you can overlay them because there is a transparency layer unlike a video frame.

0

u/[deleted] Oct 15 '19

[deleted]

0

u/billsil Oct 15 '19

Making animations with matplotlib?

-20

u/ship0f Oct 14 '19

It's because people who do this kind of stuff are mathematicians or physicist. Not IT peolpe.

12

u/scarfarce Oct 14 '19

But doing a screen recording is not rocket science.

Oh wait now I get it. It's not rocket science, so rocket scientists can't actually do it ;)

-21

u/Upvoteme12345 Oct 14 '19

What could you possibly be losing from the small difference between a screen recording and phone recording that’s a few seconds long

12

u/masteryod Oct 14 '19

What could you possibly be losing

Quality

-8

u/Upvoteme12345 Oct 15 '19 edited Oct 15 '19

It’s a reddit post for r/python lmfao the quality really isn’t that serious. You can clearly see what’s happening. Relax.

2

u/--o Oct 15 '19

It’s a reddit post for r/python lmfao people suggesting screen recording is easy aren't monsters. You don't need to go on a crusade about the virtues of filming screens. Relax.

1

u/javier123454321 Oct 15 '19

If you dont have a screen recording software and you just got this awesomely useless program to run, I totally get the impulse to just record it the way you know how and put it out.