Python generates arbitrary frequency sine wave mode

As follows:

def signal_xHz(A, fi, time_s, sample):
return A * np.sin(np.linspace(0, fi * time_s * 2 * np.pi , sample* time_s))

A: is the signal amplitude

fi: is the signal frequency

time_s: is the length of time (s)

sample: is the signal sampling frequency

Supplementary extension: Python FFT synthesis waveform example

Use the FFT function of the Python numpy module to synthesize rectangular waves and square waves, and increase the understanding of discrete Fourier transform.

Import module

import numpy as np
import matplotlib.pyplot as plt
Respectively, generate a periodic square wave and triangle wave program

# Generate a triangular wave sampled at size points, with a period of 1
def triangle_wave(size):
 x = np.arange(0,1,1.0/size)
 y = np.where(x<0.5, x,0)
 y = np.where(x =0.5,1-x, y)return x, y
 
def square_wave(size):
 x = np.arange(0,1,1.0/size)
 y = np.where(x<0.5,1.0,0)return x, y

The second value of the np.where function is if, and the third is else

The following program can calculate the corresponding frequency spectrum, the number of sampling points is taken to the power of 2 to facilitate FFT calculation

fft_size =256
 
# Calculate triangle wave and its FFT
x, y =triangle_wave(fft_size)
fy = np.fft.fft(y)/ fft_size

The following is to visualize the calculated frequency spectrum, and the intensity corresponding to the frequency is expressed by the decibel dp commonly used in engineering

# Draw the amplitude of the first 20 items of the FFT of the triangle wave. Since the values without the even numbers without subscripts are all 0, so take
# Infinitesimal after log, unable to draw, use np.The clip function sets the upper and lower limits of the array value to ensure correct drawing
plt.figure()
plt.plot(np.clip(20*np.log10(np.abs(fy[:20])),-120,120),"o")
plt.xlabel("frequency bin")
plt.ylabel("power(dB)")
plt.title("FFT result of triangle wave")

Next, synthesize the signal with sine and cosine functions

# Take the first n items in freqs of the result of FFT calculation to synthesize, return the result of synthesis, and calculate the waveform of loops cycles
def fft_combine(freqs, n, loops=1):
 length =len(freqs)* loops
 data = np.zeros(length)
 index = loops * np.arange(0, length,1.0)/ length *(2* np.pi)for k, p inenumerate(freqs[:n]):if k !=0: p *=2 #Except for the DC component, the other coefficients are*2
 data += np.real(p)* np.cos(k*index) #The coefficient of the cosine component is the real part
 data -= np.imag(p)* np.sin(k*index) #The coefficient of the sine component is negative imaginary part
 return index, data

Where index represents the sampling point in the spectral space

Draw the composite signal, and use the default integer representation for the x coordinate

# Draw the original triangle wave and the result of gradual synthesis with sine wave, using the sampling point as the x-axis coordinate
plt.figure()
plt.plot(y, label="original triangle", linewidth=2)for i in[0,1,3,5,7,9]:
 index, data =fft_combine(fy, i+1,2) #Calculate the synthesized waveform of two cycles
 plt.plot(data, label ="N=%s"% i)
plt.legend()
plt.title("partial Fourier series of triangle wave")
plt.show()

The above python method of generating arbitrary frequency sine waves is all the content shared by the editor. I hope to give you a reference.

Recommended Posts

Python generates arbitrary frequency sine wave mode
How does python enter interactive mode
Python requests module cookie instance analysis
Learn the basics of python interactive mode
Python generates arbitrary frequency sine wave mode
Python generates connotative pictures