Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Discrete Fourier Transform DFT





No account yet ?

Sign up to access all content




Tested in Anaconda and Python 3.7

import matplotlib.pyplot as plt
import numpy as np
 
plt.style.use('seaborn-poster')
 
# sampling rate
sr = 100
# sampling interval
ts = 1.0/sr
t = np.arange(0,1,ts)
 
freq = 1.
x = 3*np.sin(2*np.pi*freq*t)
 
freq = 4
x += np.sin(2*np.pi*freq*t)
 
freq = 7   
x += 0.5* np.sin(2*np.pi*freq*t)
 
plt.figure(figsize = (8, 6))
plt.plot(t, x, 'r')
plt.ylabel('Amplitude')
 
plt.show()
 
def DFT(x):
    """
    Function to calculate the 
    discrete Fourier Transform 
    of a 1D real-valued signal x
    """
 
    N = len(x)
    n = np.arange(N)
    k = n.reshape((N, 1))
    e = np.exp(-2j * np.pi * k * n / N)
 
    X = np.dot(e, x)
 
    return X
 
X = DFT(x)
 
# calculate the frequency
N = len(X)
n = np.arange(N)
T = N/sr
freq = n/T 
 
plt.figure(figsize = (8, 6))
plt.stem(freq, abs(X), 'b', \
         markerfmt=" ", basefmt="-b")
plt.xlabel('Freq (Hz)')
plt.ylabel('DFT Amplitude |X(freq)|')
plt.show()
 
n_oneside = N//2
# get the one side frequency
f_oneside = freq[:n_oneside]
 
# normalize the amplitude
X_oneside =X[:n_oneside]/n_oneside
 
plt.figure(figsize = (12, 6))
plt.subplot(121)
plt.stem(f_oneside, abs(X_oneside), 'b', \
         markerfmt=" ", basefmt="-b")
plt.xlabel('Freq (Hz)')
plt.ylabel('DFT Amplitude |X(freq)|')
 
plt.subplot(122)
plt.stem(f_oneside, abs(X_oneside), 'b', \
         markerfmt=" ", basefmt="-b")
plt.xlabel('Freq (Hz)')
plt.xlim(0, 10)
plt.tight_layout()
plt.show()
 
def gen_sig(sr):
    '''
    function to generate
    a simple 1D signal with
    different sampling rate
    '''
    ts = 1.0/sr
    t = np.arange(0,1,ts)
 
    freq = 1.
    x = 3*np.sin(2*np.pi*freq*t)
    return x
 


Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com












Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !

Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License