Signal processing in Python (1): Fourier transform

Introduction

Study material for signal processing in Python.

Library to use

numpy, matplotlib scipy fftpack

Execution content

  1. Generate a sine wave of a specific frequency. (numpy)
  2. Run the FFT. (scipy)
  3. Plot the execution result. (matplotlib)

Program code

1. Sine wave generation part

N = 2**20 # data number
dt = 0.0001 # data step [s]
f1 = 5 # frequency[Hz]
A1 = 1 # Amplitude
p1 = 90*pi/180  # phase [rad]

#Waveform formation
t = np.arange(0, N*dt, dt) # time
freq = np.linspace(0, 1.0/dt, N) # frequency step
y = A1*np.sin(2*np.pi*f1*t + p1) 

2. Fourier transform part

#Discrete Fourier transform&Standardization
yf = fft(y)/(N/2) 

# y :numpy array
# N :Number of samplings

Whole code

fft.py


import numpy as np
from scipy.fftpack import fft
import matplotlib.pyplot as plt

#Pi
pi = np.pi

# parameters

N = 2**20 # data number
dt = 0.0001 # data step [s]
f1 = 5 # frequency[Hz]
A1 = 1 # Amplitude
p1 = 90*pi/180 # phase

#Waveform formation
t = np.arange(0, N*dt, dt) # time
freq = np.linspace(0, 1.0/dt, N) # frequency step

y = A1*np.sin(2*np.pi*f1*t + p1)

#Fourier transform
yf = fft(y)/(N/2) #Discrete Fourier transform&Standardization

#plot
plt.figure(2)

plt.subplot(211)
plt.plot(t, y)
plt.xlim(0, 1)
plt.xlabel("time")
plt.ylabel("amplitude")

plt.subplot(212)
plt.plot(freq, np.abs(yf))
plt.xlim(0, 10)
plt.xlabel("frequency")
plt.ylabel("amplitude")

plt.tight_layout()
plt.savefig("01")

plt.show()

3. Execution result

Top: Time domain graph: Confirm that the phase is 90 degrees out of phase. Bottom: Frequency domain graph: 5Hz frequency confirmed. Figure_2.png

Summary

  1. I ran the FFT in Python.

Impressions

I've used matlab for signal processing so far, but python's scipy is easier.

Reference material

Recommended Posts

Signal processing in Python (1): Fourier transform
File processing in Python
Multithreaded processing in python
Text processing in Python
Queue processing in Python
Image processing from scratch with python (5) Fourier transform
UTF8 text processing in python
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Asynchronous processing (threading) in python
Image Processing Collection in Python
Using Python mode in Processing
100 Language Processing Knock Chapter 1 in Python
Fourier series verification code written in Python
Easy image processing in Python with Pillow
Duplicate prohibition processing in GAE / Python Datastore
Status of each Python processing system in 2020
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
python image processing
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Python file processing
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python