[PYTHON] Try to generate an image with aliasing

Introduction

When converting an analog signal to a digital signal, it is possible to observe only frequencies up to 1/2 the sampling frequency $ f_s $. This frequency is called the Nyquist frequency, but it is known that it cannot be observed even if it contains vibrations that exceed this Nyquist frequency. Vibrations above this Nyquist frequency are treated as low frequencies. Treating high-frequency vibrations that exceed the Nyquist frequency as low frequencies is called aliasing (aliasing).

Considering the discrete Fourier transform of data with $ N $ numbers from $ x_0 $ to $ x_ {N-1} $

C_k = \frac{1}{N}\sum_{m=0}^{N-1} x_me^{-i\frac{2\pi km}{N}}
k = 0, 1, 2, …, N-1

The frequency corresponding to k is

f_k = \frac{kf_s}{N}

It will be. Since k exists up to (N-1), it becomes $ f_ {N-1} = \ frac {(N-1) f_s} {N} $, and vibrations exceeding the Nyquist frequency of $ f_s / 2 $ can be expressed. That's right ..., $ C_k $ and $ C_ {Nk} $ are known to be complex conjugates of each other.

C_k = C_{N-k}^*

Therefore, wrapping will occur centered on $ f_s / 2 $. Don't be afraid to misunderstand that the $ C_ {N-k} $ wave will be treated as a $ C_k $ frequency wave.

Image generation

Use numpy to generate high resolution images (5000x5000). This is regarded as a pseudo analog signal.


import numpy as np
import matplotlib.pyplot as plt

#Specify the size of the image
img_size_origin = 5000
#Zero matrix as an image
img = np.zeros([img_size_origin, img_size_origin])

#Image pixel values depend on y coordinate
y = np.arange(img_size_origin) 

#Period 100 pixels
amp = np.sin(2*np.pi*0.01*y)
for i in range(img_size_origin):
    img[:, i] = amp
    
plt.imshow(img, cmap="gray")
plt.show()

This will generate an image with 50 stripes. Since it has 50 stripes, if the entire image is regarded as one cycle, there are 50 Hz waves.

ig.png

Extract only 1 pixel per [comp x comp] from this image and generate a new image. If comp = 5, the extracted pixels will have coordinates of [3 + 5n, 3 + 5m]. (n> = 0, m> = 0)

comp = 99
#Calculate the image size of the output
img_size = img.shape[0]//comp
print(img_size)
#Output image
img_comp = np.zeros([img_size, img_size])

#[comp×comp]Extract only the center pixel from the pixels of
for i in range(img_size):
    for j in range(img_size):
        img_comp[i, j] = img[(i*comp) + (comp//2), (j*comp) + (comp//2)]

plt.imshow(img_comp, cmap="gray")
plt.show()

plt.plot(img_comp[:, 0])
plt.show()

If comp is 99

I couldn't observe the 50 Hz wave well, and it became a 0.5 Hz wave. img_comp.png

When comp is 98

I couldn't observe the 50 Hz wave well, and it became a 1 Hz wave. img_comp.png

When comp is 96

I couldn't observe the 50 Hz wave well, and it became a 2 Hz wave.

img_comp.png

At the end

It seems that a lot of mysterious images can be made (small average feeling)

Recommended Posts

Try to generate an image with aliasing
Try to extract a character string from an image with Python3
How to crop an image with Python + OpenCV
Try to automatically generate Python documents with Sphinx
Post an article with an image to WordPress with Python
I want to convert an image to WebP with lollipop
Try to factorial with recursion
[Evangelion] Try to automatically generate Asuka-like lines with Deep Learning
Image analysis with Object Detection API to try in 1 hour
Cut out an image with python
Try to operate Facebook with Python
Convert a string to an image
Try blurring the image with opencv2
Convert PDF to image with ImageMagick
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
Try to create an execution path diff viewer with angr + bingraphvis
I tried to make an image similarity function with Python + OpenCV
Try to generate a death metal jacket image with DCGAN + scrape the metal database site for that
Try to reproduce color film with Python
Creating an image splitting app with Tkinter
HTML email with image to send with python
Image classification with Keras-From preprocessing to classification test-
Create an image processing viewer with PySimpleGUI
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Try to predict cherry blossoms with xgboost
Quickly try to visualize datasets with pandas
Easily try automatic image generation with DCGAN-tensorflow
First YDK to try with Cisco IOS-XE
Sample to convert image to Wavelet with Python
[TCP / IP] After studying, try to make an HTTP client-like with Python
Try to image the elevation data of the Geographical Survey Institute with Python
[AWS] How to deal with WordPress "An error occurred when cropping an image."
Try to make your own AWS-SDK with bash
Try to solve the fizzbuzz problem with Keras
Library comparison summary to generate PDF with Python
Try adding fisheye lens distortion to the image
Crop the image to rounded corners with pythonista
Try to aggregate doujin music data with pandas
Convert PDF to image (JPEG / PNG) with Python
Generate and post dummy image data with Django
I tried to get an image by scraping
Add rows to an empty array with numpy
Send an email to Spushi's address with python
Create an image composition app with Flask + Pillow
Try to solve the man-machine chart with Python
Try to extract Azure document DB document with pydocumentdb
Try to draw a life curve with python
Try to communicate with EV3 and PC! (MQTT)
I want to be an OREMO with setParam!
How to try the friends-of-friends algorithm with pyfof
Try montage your face with an AB test
Try to make a "cryptanalysis" cipher with Python
How to read an array with Python's ConfigParser
Create an image with characters in python (Japanese)
[Python] Automatically generate an example animation list-like image.
I tried to extract a line art from an image with Deep Learning
I tried to detect an object with M2Det!
Try to make a dihedral group with Python
Try to create an HTTP server using Node.js
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
Try building an environment for MayaPython with VisualStudioCode