In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how Python generates arbitrary waveforms and coexists as txt, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
one。 Script function
According to the parameters such as the number of sampling points, the number of sampling periods and the mathematical expression of waveform, arbitrary waveform is generated.
Convert the waveform data to a binary complement with a specified bit width, and then save it as txt
Draw the original waveform and the waveform converted to binary complement to verify whether the conversion is correct.
two。 Use effect
three。 Code sharing''Author: Xu DakangEmail: XudaKang_up@qq.comDate: 2021-11-19 19:12:31LastEditors: Xu DakangLastEditTime: 2021-11-21 21:36:19Filename: Description:' module function: 1. According to the parameters such as the number of sampling points, the number of sampling periods and the mathematical expression of the waveform, any waveform 2. Convert the waveform data into a binary complement with a specified bit width, and then save it as txt3. Draw the original waveform and convert it to binary complement Verify that the conversion is correct''import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.pylab import mplmpl.rcParams [' font.sans-serif'] = ['SimHei'] # shows the Chinese mpl.rcParams [' axes.unicode_minus'] = False # shows the negative sign import mplcursorsimport timenow_time = time.strftime ("% Y%m%d-%H%M%S", time.localtime (time.time () # my self-designed module import myBin2dec2hexforth! Parameter file_name = 'waveform-' + now_timeN = 250 # sampling points per cycle, sampling frequency Fs = signal frequency f * sampling points NTNUM = 10 # sampling periods BIT_WIDTH = 24 # binary complement bits f = 0.5 # sinusoidal signal frequency, arbitrary value t0 = np.linspace (0,1 / f, N) # np.linspace (start, end, number) Note that the start point will be included and the end point may be included (if divisible) pi = NP. Change the expression of y0 to get an arbitrary waveform # sine function formula y = sin (wt) = sin (2π ft) y0 = np.sin (2 * pi * f * t0) + np.sin (2 * pi * f * 2 * t0) # y0 = np.sin (2 * pi * f * t0) #! Periodic extension of original waveform The extended waveform 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) plt.figure (1) plt.subplot (2,1,1) plt.plot (x0_tnum, y0_tnum) plt.grid () plt.title ('original waveform The minimum frequency ='+ str (f) + 'corresponds to the period' + str (1 beat f) +', the number of periods ='+ str (TNUM) +', and the sampling frequency ='+ str (f * N)) mplcursors.cursor () # makes it possible to take points on the image #! The decimal of the original waveform is multiplied by the binary magnification and then rounded up y0_bit = np.int0 ((2cm * (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) #! Transfer the binary complement from 10 to 2 and store it in the txt file fotxt =''fo = open (file_name + '.txt', 'wicked, encoding='utf8') for dec_num in y0_bit_tnum: # excluding the last number fotxt + = myBin2dec2hex.signed_dec2bin (dec_num, BIT_WIDTH) [2:] +'\ n'fo.write (fotxt [:-1]) print ('generate' + file_name + '.txt file successfully!') Fo.close () #! Read the written txt file, convert it to decimal, and draw the waveform To verify that the write is correct fi = open (file_name + '.txt', 'ritual, encoding='utf8') y_out = [] for line in fi.readlines (): y_out.append (myBin2dec2hex.signed_bin2dec (line)) fi.close () x_out = list (range (len (x0_tnum) plt.figure (1) plt.subplot (2, 1, 2) plt.plot (x_out Y_out) plt.grid () plt.title ('convert the binary complement first and then the decimal waveform, which is equivalent to the decimal value multiplied by 2 ^' + str (BIT_WIDTH-1) +'- 1, that is,'+ str ('{: e} '.format (2 steps * (BIT_WIDTH-1)-1)) mplcursors.cursor () plt.show () Thank you for reading this article carefully. I hope the article "how to generate arbitrary waveforms and co-exist as txt" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.