[PYTHON] Animate what happens in frequency space when the Nyquist frequency is exceeded

Introduction

There is a sampling theorem (sampling theorem). By sampling twice for one wavelength, the original signal can be completely restored. This sampling interval is called the sampling rate, and half the frequency is called the Nyquist frequency. Aliasing occurs when the signal to be measured exceeds the Nyquist frequency. Even if I knew this kind of thing as knowledge, I thought that I had never seen an animation of what it would look like in the frequency space beyond the Nyquist frequency, so I made it.

reference

I read it to recall the sampling theorem. Ono Sokki: About FFT Analyzer (page4)

program

import numpy as np
import matplotlib.pyplot as plt

N = 512 #FFT sample score
fs = 2000.0 #Sampling rate
t = np.linspace(0.0,N/fs,N)
fft_freq = np.linspace(0.0,fs,N) #FFT frequency axis
f_array = np.linspace(10.0,4000.0,200)

for i,f in enumerate(f_array):
    sig = np.sin(2.0*np.pi*f*t) * np.hamming(N)
    fft_amp = np.abs(np.fft.fft(sig)) / N * 2.0 / 0.54
    
    fig,axes = plt.subplots()
    axes.plot(fft_freq,fft_amp,label="{} Hz".format(np.round(f)))
    axes.set_xlabel("Frequency Hz")
    axes.set_ylabel("Amplitude")
    axes.set_ylim(0.0,1.1)
    axes.legend(loc="upper right")
    fig.savefig("{}.png ".format(i))
    plt.close()

The serial number photos were converted to animation with GIMP.

result

It moves like a mirror at the Nyquist frequency. This will give you a good idea of why you shouldn't exceed the Nyquist frequency. You can see the frequency this time, but if you look only at the result, you can't tell whether it was folded or measured correctly. ナイキスト周波数_エイリアシング.gif

Summary

Even if you remember that you shouldn't exceed the Nyquist frequency, you'll rarely see what happens if you do. I hope it helps someone.

Recommended Posts

Animate what happens in frequency space when the Nyquist frequency is exceeded
Check in advance what happens when you execute the command
What to do when the value type is ambiguous in Python?
What is "mahjong" in the Python library? ??
When the target is Ubuntu 16.04 in Ansible
What to do when the result downloaded via scrapy is in English
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
What is wheezy in the Docker Python image?
[Question] What happens when I use% in python?
What happens to the access log when the website is automatically accessed by selenium-webdriver
When the selected object in bpy.context.selected_objects is not returned
What to do when is not in the sudoers file.This incident will be reported.
What to do when only the window is displayed and nothing is displayed in pygame Note
What is the domain attribute written in Plotly's Layout?
Embedding in datetime when only the time is known
What is the activation function?
What is the interface for ...
What is the Callback function?
How to give and what the constraints option in scipy.optimize.minimize is
[Linux] When the screen is cut off when installing CentOS in VirtualBox
[Golang] "package exec is not in GOROOT" when executing the test
The solution when an empty object is returned in go's json.Marshal
What happens when I change the hyperparameters of SVM (RBF kernel)?
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
[python] What is the sorted key?
What is the X Window System?
What is the python underscore (_) for?
When the node disappears in rqt_graph
Is the space replaced by a plus sign or% 20 in percent-encoding processing?
What kind of book is the best-selling "Python Crash Course" in the world?
Notify using Notification Center when the execution environment is macOS in Python
What is the fastest way to create a reverse dictionary in python?
What to do when a warning message is displayed in pip list