Sunday, June 14, 2020

Learn Thou zip Ahmet Taspinar!

And watch Jeff Knupp's talk (or read the Writing Idiomatic Python book :) as well :)

Obviously this guy is super smart, else Nikhil wouldn't be referring us to him..

But, ..

x_value = np.linspace( 0, t_n, N)
amplitudes = [4, 6, 8, 10, 14]
frequencies = [6.5, 5, 3, 1.5, 1]
y_vals = [amplitudes[ii] * np.sin( 2 * np.pi * frequencies[ii] * x_value) for ii in range( 0 , len(amplitudes))]
y_total = np.sum( y_vals, axis=0 )

Yikes! I learnt better from Mr. NB :

y_values = [ A * np.sin( 2 * np.pi * f * x_value) for A,f in zip( amplitudes, frequencies)]
y_composite = np.sum( y_values, axis=0 )

Keep it readable and idiomatic! That's the python way

Easy to verify both give you the same.. as they should.

OMG!!!!!!! How did this guy even graduate?

In the get_fft_values function above, the scipy.fftpack.fft function returns a vector of complex valued frequencies. Since they are complex valued, they will contain a real and an imaginary part. The real part of the complex value corresponds with the magnitude, and the imaginary part with the phase of the signal. Since we are only interested in the magnitude of the amplitudes, we use np.abs() to take the real part of the frequency spectrum.

Jeez!!

No comments: