Skip to main content

Fourier Transform - Basics - Example

 U might wonder how fft is performed using python or any using any language

the concept here is to explain the fft in detail with example

u can run the code in python using colab or copy the code below to run in your local m/c.


Consider a sample of data  that  has N data points against time

the fft for the data points will give you the amplitude for the data points

which could a  complex number 

again what is a complex number (a+ib)

i*i = -1

again why this complexity don't worry too much we will talk about this in detail.

consider 

a - to be the x axis (real)

ib - y axis (Imaginary)







still confused with the formula don't worry we will work a sample for f(0)


so now we have seen the computation for f(0)

in the same way calculate till f(N-1) ,run the code below to cross check the values.

 result -

f(0) = (-1.1102230246251565e-16+0j)
f(1) = (5.551115123125783e-16-3.9996979771955568j)
f(2) = (-1.410503594010501e-16+2.220446049250313e-16j)
f(3)= 0.00030202280444291407j
f(4) = 2.0280150993880174e-16j
f(5) = (1.3322676295501878e-15-0.0003020228044421369j)
f(6) = (2.4561914808967368e-15+7.771561172376096e-16j)
f(7)=  (-2.9976021664879227e-15+3.999697977195556j)

 all these are complex numbers dont worry 

amplitude can be calculated using the formula 

amplitude = sqrt(a^2+ib^2)


The frequency is tipped at f(1) and f(7) giving as the frequency of the signal as 1Hz

this way we can do a fft for mix of sin signals to separate various frequencies 

and do a data manipulation such as filtering certain frequencies

which are considered as noise.

enjoy fft with python.

https://github.com/naveez-alagarsamy/matplotlib/blob/main/Fourier_basics.ipynb

python code - 

 

 

import math
import numpy as np
import matplotlib.pyplot as plt

print(math.e**(np.pi/4*1j))

#loop through the samples -use size
# euler's formula   ---- e^i*x  = cosx +isinx

#fft - theta= 2pikn/N

# k - 0---N-1
#n- loop for all samples
#N - total no of samples

#fft [k] = summation (0---N-1)[x[k]*e^(-i*theta)]
# fft [k] = summation (0---N-1)[x[k]* (cos(theta) -i*sin(theta))

x = [0, 0.707, 1, 0.707, 0, -0.707, -1, -0.707]
N = len(x)

def calculate_theta(k, n , N):
  theta = (2*np.pi*k*n)/N
  return theta

def calculate_eulerfor_theta(theta): 
  #print(math.e**(theta * -1j))
  euler = math.e**(theta * -1j)
  return euler

x_n_fft = []

for i in range(N):
  #print(i)
    
  x_n=0
  for j in range(N):
    thetas = calculate_theta(i, j, N)
    eulers = calculate_eulerfor_theta(thetas)
    x_n += x[j]* eulers
  print(round(abs(x_n)))
  x_n_fft = np.append(x_n_fft, round(abs(x_n)))
print(x_n_fft)


#plot the frequency domain graph 
#x - axis  -  freq
#y - axis  - amplitude 

#Amplitude is the magnitude from FFT --x_n_fft values
#angular frquency = [2pi*n/sample_interval * N ]
#sample_interval = [2pi/N]

sample_interval = (2*np.pi)/N
angular_frequencys = []
for angular_frequency_fft in range(4):
  angular_frequency = (2*np.pi*angular_frequency_fft/(sample_interval*N))
  angular_frequencys.append(angular_frequency)

print(angular_frequencys)

plt.plot(angular_frequencys, x_n_fft[0:4])
plt.show()

# u might wonder why i used 4 in range rather than N 
#the values start repeating after N/2 
# u can use N and verify



Comments

Popular posts from this blog

SHA-256 initial values

The simple workout to arrive at the initial values for sha-256 The first 32 bit of the fractional part of the sqroot (first 8 prime number 2-19) Alright what does it say  Sqrroot(prime)- Let’s say the first prime is 2 Sqroot(2)  = 1.414213562373095 Convert to hexadecimal- Since we are worried about the fractional part alone Converting the fractional part would be easy Fractional part- 0.414213562373095 Multiply the fractional part with 16 to arrive at hex 0.414213562373095*16= 6.62741699796952 0.62741699796952*16= 10.03867196751232 0.03867196751232*16=0.61875148019712 0.61875148019712*16=9.90002368315392 0.90002368315392*16=14.40037893046272 0.40037893046272*16=6.40606288740352 0.40606288740352*16=6.49700619845632 0.49700619845632*16=7.95209917530112 Resulting hexadecimal would be 6a09e667 which is  h0 := 0x6a09e667 Iam going to stop at the 8th iteration , why is that ? Since we are interested in 32 bit (8*4=32) Alright to make it clear  Convert hexade...

Linear Regression with one variable - Introduction

 It is not but making a some how clear relationship among variables the dependent and independent variables. talking in terms of maths the equation can be used meaningfully for something may be to determine /predict values from data. if y = m * x + b  the values for m , b can be anything but has to appropriate to predict y  so the loss which is  difference from existing to prediction is close to zero ~0 to start with we can say the one variable as -x  in some scenario m , b are called variables    the equation stated about is a line equation we have any equation  y = 2*x  y = x*x y = 2x +2x*x  so why the need of all these equations , it is all about playing data now a days in machine learning problems we create a data sets , lets consider as x  y to be a value of x the datas . y = datas  when we express the data as a function and plot in the graph we get the curves  take some random data x and plot x and y  x =1 , 2, ...

Running node in browser-https://stackblitz.com

 Here comes the web browser development for web apps you can run your node project in web and do development as well  please tryout the node or any other web development using  https://stackblitz.com/ https://stackblitz.com/ run your scripts in the terminal Import your project from git hub and enjoy web over web development. see you soon.