pyqt5:py handles the conversion of C language format arrays and signed numbers (memo)

Article directory

  • 1. Question: Draw the sine wave represented by the following array with a curve.
    • 1.1 Replace the C language array directly with the py array
    • 1.2 Use numpy to read into the array
    • 1.3 Complete code
  • 2. Generate C array from sine wave data
    • 2.1 Sine wave data
    • 2.2 The formula for converting negative numbers into 16-bit signed integers
    • 2.3 Convert negative numbers to 16-bit signed integer complete code
  • 3. Code to generate sine wave data

AB32VG1 outputs sine wave source code on the audio port:
sddac_test.c

1. Question: Draw the sine wave represented by the following array with a curve.

AB32VG1 is little-endian memory.

const unsigned char hexData[96] = {<!-- -->
    0x00, 0x00, 0xB5, 0x10, 0x21, 0x21, 0xFC, 0x30, 0x00, 0x40, 0xEC, 0x4D, 0x82, 0x5A, 0x8D, 0x65,
    0xDA, 0x6E, 0x42, 0x76, 0xA3, 0x7B, 0xE8, 0x7E, 0xFF, 0x7F, 0xE8, 0x7E, 0xA3, 0x7B, 0x42, 0x76,
    0xDA, 0x6E, 0x8D, 0x65, 0x82, 0x5A, 0xEC, 0x4D, 0x00, 0x40, 0xFC, 0x30, 0x21, 0x21, 0xB5, 0x10,
    0x00, 0x00, 0x4B, 0xEF, 0xDF, 0xDE, 0x04, 0xCF, 0x00, 0xC0, 0x14, 0xB2, 0x7E, 0xA5, 0x73, 0x9A,
    0x26, 0x91, 0xBE, 0x89, 0x5D, 0x84, 0x18, 0x81, 0x00, 0x80, 0x18, 0x81, 0x5D, 0x84, 0xBE, 0x89,
    0x26, 0x91, 0x73, 0x9A, 0x7E, 0xA5, 0x14, 0xB2, 0x00, 0xC0, 0x04, 0xCF, 0xDF, 0xDE, 0x4B, 0xEF
};

After conversion:

1.1 Replace the C language array directly with the py array

python array:

hexData= [
    0x00, 0x00, 0xB5, 0x10, 0x21, 0x21, 0xFC, 0x30, 0x00, 0x40, 0xEC, 0x4D, 0x82, 0x5A, 0x8D, 0x65,
    0xDA, 0x6E, 0x42, 0x76, 0xA3, 0x7B, 0xE8, 0x7E, 0xFF, 0x7F, 0xE8, 0x7E, 0xA3, 0x7B, 0x42, 0x76,
    0xDA, 0x6E, 0x8D, 0x65, 0x82, 0x5A, 0xEC, 0x4D, 0x00, 0x40, 0xFC, 0x30, 0x21, 0x21, 0xB5, 0x10,
    0x00, 0x00, 0x4B, 0xEF, 0xDF, 0xDE, 0x04, 0xCF, 0x00, 0xC0, 0x14, 0xB2, 0x7E, 0xA5, 0x73, 0x9A,
    0x26, 0x91, 0xBE, 0x89, 0x5D, 0x84, 0x18, 0x81, 0x00, 0x80, 0x18, 0x81, 0x5D, 0x84, 0xBE, 0x89,
    0x26, 0x91, 0x73, 0x9A, 0x7E, 0xA5, 0x14, 0xB2, 0x00, 0xC0, 0x04, 0xCF, 0xDF, 0xDE, 0x4B, 0xEF
   ]

1.2 Use numpy to read into the array

AB32VG1 is little-endian storage, that is, the low byte is stored in the low address. Take the last set of data hexData[94,95]=0x4B, 0xEF as an example for analysis.
hexData[95]= 0xEF is the high byte, so the calculation is as follows:
dat = 0xEF*256 + 0x4B = 0xEF4B. At this time, it is printed out as decimal 61259:

>>>print(dat)
61259

Python does not have a 16-bit signed integer, so it needs to be converted to a corresponding negative number:

dat = dat -0x10000

print again:

>>>print(dat)
-4277

1.3 complete code

import matplotlib.pyplot as plt #drawing package
import numpy as np
# 1. Data copied from C language
hexData = [
    0x00, 0x00, 0xB5, 0x10, 0x21, 0x21, 0xFC, 0x30, 0x00, 0x40, 0xEC, 0x4D, 0x82, 0x5A, 0x8D, 0x65,
    0xDA, 0x6E, 0x42, 0x76, 0xA3, 0x7B, 0xE8, 0x7E, 0xFF, 0x7F, 0xE8, 0x7E, 0xA3, 0x7B, 0x42, 0x76,
    0xDA, 0x6E, 0x8D, 0x65, 0x82, 0x5A, 0xEC, 0x4D, 0x00, 0x40, 0xFC, 0x30, 0x21, 0x21, 0xB5, 0x10,
    0x00, 0x00, 0x4B, 0xEF, 0xDF, 0xDE, 0x04, 0xCF, 0x00, 0xC0, 0x14, 0xB2, 0x7E, 0xA5, 0x73, 0x9A,
    0x26, 0x91, 0xBE, 0x89, 0x5D, 0x84, 0x18, 0x81, 0x00, 0x80, 0x18, 0x81, 0x5D, 0x84, 0xBE, 0x89,
    0x26, 0x91, 0x73, 0x9A, 0x7E, 0xA5, 0x14, 0xB2, 0x00, 0xC0, 0x04, 0xCF, 0xDF, 0xDE, 0x4B, 0xEF
   ]

# 2. Generate x-axis data
cnt = 48
x=np.linspace(1, cnt, cnt, dtype=int)

# 3. Generate y-axis empty data
y=np.empty([48], dtype = int, order = 'C')
dat=0
i=0
j=0
# 4. Generate y-axis data
while i < len(hexData):
    dat=hexData[i + 1]*256 + hexData[i]
    # Values greater than 0x7fff are negative
    if dat > 0x7fff :
        y[j]=dat-0x10000
    else:
        y[j]=dat
    print(dat)
    i + = 2
    j + =1
# 5. Drawing
plt.plot(x,y,'bp--') #drawn into a chart
plt. show()
# 6. Write to text archive
np.savetxt("pcm001-48.txt",(y),delimiter=',',fmt="%d",newline='\\
')

2. Generate C array from sine wave data

2.1 Sine wave data

The corresponding data of the above C array is as follows:

0 ,
4277 ,
8481 ,
12540 ,
16384 ,
19948,
23170 ,
25997 ,
28378 ,
30274 ,
31651 ,
32488 ,
32767 ,
32488 ,
31651 ,
30274 ,
28378 ,
25997 ,
23170 ,
19948,
16384 ,
12540 ,
8481 ,
4277 ,
0 ,
-4277 ,
-8481,
-12540 ,
-16384 ,
-19948 ,
-23170 ,
-25997 ,
-28378 ,
-30274,
-31651 ,
-32488 ,
-32768 ,
-32488 ,
-31651 ,
-30274,
-28378 ,
-25997 ,
-23170 ,
-19948 ,
-16384 ,
-12540 ,
-8481,
-4277

2.2 Convert negative numbers to 16-bit signed integer formula

The formula for converting a 16-bit signed integer representing a negative number into a negative number is as follows:

if dat > 0x7fff:
dat = dat-0x10000

Here is the inverse operation:

 if dat <0 :
        dat=dat + 0x10000

2.3 Convert negative numbers to 16-bit signed integer complete code

Note that storage is little-endian.

import numpy as np
# 1. Sine wave data
hexData = [
    0 ,
    4277 ,
    8481 ,
    12540 ,
    16384 ,
    19948,
    23170 ,
    25997 ,
    28378 ,
    30274 ,
    31651 ,
    32488 ,
    32767 ,
    32488 ,
    31651 ,
    30274 ,
    28378 ,
    25997 ,
    23170 ,
    19948,
    16384 ,
    12540 ,
    8481 ,
    4277 ,
    0 ,
    -4277 ,
    -8481,
    -12540 ,
    -16384 ,
    -19948 ,
    -23170 ,
    -25997 ,
    -28378 ,
    -30274,
    -31651 ,
    -32488 ,
    -32768 ,
    -32488 ,
    -31651 ,
    -30274,
    -28378 ,
    -25997 ,
    -23170 ,
    -19948 ,
    -16384 ,
    -12540 ,
    -8481,
    -4277
   ]
i=0
# 2. Generate 96 data storage spaces
y=np.empty([96], dtype = int, order = 'C')
dat=0
j=0
while i < len(hexData):
    dat=hexData[i]
    if dat <0 :
        dat=dat + 0x10000
    
    print(dat)
    y[j] = dat % 256
    j + =1
    y[j] = dat / 256
    j + =1
    i + = 1
# 3. Save to text
np.savetxt("pcm001-96.txt",(y),delimiter=',',fmt="0x x",newline='\\
')

3. Code to generate sine wave data

import matplotlib.pyplot as plt #drawing package
import numpy as np

# 1. Take 48 points of equal length from 0 - 2 pi as the x-axis data
x=np.linspace(0*np.pi,2*np.pi,12*4) #
# 2. Generate y-axis data based on x-axis
y=32768*np.sin(x) #Adjust the value of 32768 to adjust the amplitude
# 3. rounding
y1=y.astype(int) #Convert the y value to an integer
# 4. Draw as a graph
plt.plot(x,y1,'bp--')
plt. show()

# 5. Save sine wave data to txt file
np.savetxt("pcm-48.txt",(y1),delimiter=',',fmt="%d",newline='\\
')

The generated data 《pcm-48.txt》 is as follows:

0
4367
8657
12792
16699
20307
23554
26380
28735
30578
31875
32603
32749
32311
31296
29723
27619
25023
21980
18544
14778
10748
6526
2188
-2188
-6526
-10748
-14778
-18544
-21980
-25023
-27619
-29723
-31296
-32311
-32749
-32603
-31875
-30578
-28735
-26380
-23554
-20307
-16699
-12792
-8657
-4367
0