I. Script Functions
- Generate arbitrary waveforms based on the number of sampling points, the number of sampling periods and other parameters and the mathematical expression of the waveform.
- Convert the waveform data to binary complement of the specified bit width, then save as txt
- Plot the original waveform and the waveform after conversion to binary complement to verify that the conversion is correct
II. Effectiveness of use
III. Code Sharing
'''
Author : Xu Dakang
Email : XudaKang_up@
Date : 2021-11-19 19:12:31
LastEditors : Xu Dakang
LastEditTime : 2021-11-21 21:36:19
Filename :
Description :
'''
'''
Module function:
1. According to the number of sampling points, the number of sampling periods and other parameters and the mathematical expression of the waveform, to generate arbitrary waveforms
2. Convert the waveform data into binary complement of specified bit width, and then save it as txt.
3. Draw the original waveform and the waveform after conversion to binary complement to verify whether the conversion is correct.
'''
import numpy as np
import as plt
from import mpl
['-serif'] = ['SimHei'] #Display Chinese
['axes.unicode_minus'] = False #Display negative sign
import mplcursors
import time
now_time = ("%Y%m%d-%H%M%S", (()))
# My self-programmed modules
import myBin2dec2hex
#! Parameters to be changed
file_name = 'waveform-' + now_time
N = 250 # of samples in a cycle, sampling frequency Fs = signal frequency f * number of samples N
TNUM = 10 # of sampling cycles
BIT_WIDTH = 24 # of binary complement bits
f = 0.5 # Frequency of the sine wave, can take any value
t0 = (0, 1 / f, N) # (start, end, number), note that the start point will be included and the end point may be included (if divisible)
pi =
#! Change the expression for y0 to get an arbitrary waveform
# Sine function formula y = sin(wt) = sin(2πft)
y0 = (2 * pi * f * t0) + (2 * pi * f * 2 * t0)
# y0 = (2 * pi * f * t0)
#! The original waveform cycle is delayed and the delayed waveform is plotted
x0_tnum = []
y0_tnum = []
y0_bit_tnum = []
for i in range(TNUM):
for j in t0:
x0_tnum.append(j + i * 1 / f)
for k in y0:
y0_tnum.append(k)
(1)
(2, 1, 1)
(x0_tnum, y0_tnum)
()
('raw waveform,minimum frequency = ' + str(f) + ' corresponds to the period ' + str(1/f)
+ ',periodic number = ' + str(TNUM) + ',sampling frequency = ' + str(f * N))
() # Make it possible to take points on the image
#! Raw waveform decimals multiplied by binary magnification and rounded to the nearest whole number
y0_bit = np.int0((2**(BIT_WIDTH - 1) - 1) * y0 / max(abs(y0)))
y0_bit_tnum = []
for i in range(TNUM):
for j in y0_bit:
y0_bit_tnum.append(j)
#! Convert binary 10 to binary 2's complement, then store in txt file
fotxt = ''
fo = open(file_name + '.txt', 'w', encoding='utf8')
for dec_num in y0_bit_tnum: # Excluding the last number
fotxt += myBin2dec2hex.signed_dec2bin(dec_num, BIT_WIDTH)[2:] + '\n'
(fotxt[:-1])
print('Generate' + file_name + '.txt file succeeded!')
()
#! Read the written txt file, convert to decimal and draw a waveform to verify that the write is correct
fi = open(file_name + '.txt', 'r', encoding='utf8')
y_out = []
for line in ():
y_out.append(myBin2dec2hex.signed_bin2dec(line))
()
x_out = list(range(len(x0_tnum)))
(1)
(2, 1, 2)
(x_out, y_out)
()
('Convert binary complement and then convert to decimal post wave, equivalent to multiplying decimal value by 2^' + str(BIT_WIDTH - 1) + ' - 1 is '
+ str('{:e}'.format(2**(BIT_WIDTH - 1) - 1)))
()
()
to this article on Python generate arbitrary waveforms and save as txt implementation of the article is introduced to this, more relevant Python generate arbitrary waveforms content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!