1import cmsisdsp as dsp 2import numpy as np 3from scipy import signal 4import matplotlib.pyplot as plt 5from scipy.fftpack import dct 6 7r = dsp.arm_add_f32(np.array([1.,2,3]),np.array([4.,5,7])) 8print(r) 9 10r = dsp.arm_add_q31([1,2,3],[4,5,7]) 11print(r) 12 13r = dsp.arm_add_q15([1,2,3],[4,5,7]) 14print(r) 15 16r = dsp.arm_add_q7([-1,2,3],[4,127,7]) 17print(r) 18 19r = dsp.arm_scale_f32([1.,2,3],2) 20print(r) 21 22r = dsp.arm_scale_q31([0x7FFF,0x3FFF,0x1FFF],1 << 20,2) 23print(r) 24 25r = dsp.arm_scale_q15([0x7FFF,0x3FFF,0x1FFF],1 << 10,2) 26print(r) 27 28r = dsp.arm_scale_q7([0x7F,0x3F,0x1F],1 << 5,2) 29print(r) 30 31 32r = dsp.arm_negate_f32([1.,2,3]) 33print(r) 34 35r = dsp.arm_negate_q31([1,2,3]) 36print(r) 37 38r = dsp.arm_negate_q15([1,2,3]) 39print(r) 40 41r = dsp.arm_negate_q7(np.array([0x80,0x81,0x82])) 42print(r) 43 44r = dsp.arm_cmplx_conj_f32([1.,2,3,4]) 45print(r) 46 47r = dsp.arm_cmplx_conj_q31([1,2,3,4]) 48print(r) 49 50r = dsp.arm_cmplx_conj_q15([1,2,3,4]) 51print(r) 52 53r = dsp.arm_cmplx_dot_prod_f32([1.,2,3,4],[1.,2,3,4]) 54print(r) 55 56r = dsp.arm_cmplx_dot_prod_q31([0x1FFF,0x3FFF,0x1FFF,0x3FFF],[0x1FFF,0x3FFF,0x1FFF,0x3FFF]) 57print(r) 58 59r = dsp.arm_cmplx_mult_real_f32([1.0,2,3,4],[5.,5.,5.,5.]) 60print(r) 61 62pidf32 = dsp.arm_pid_instance_f32(Kp=1.0,Ki=1.2,Kd=0.4) 63print(pidf32.Kp()) 64print(pidf32.Ki()) 65print(pidf32.Kd()) 66print(pidf32.A0()) 67 68dsp.arm_pid_init_f32(pidf32,0) 69print(pidf32.A0()) 70 71print(dsp.arm_cos_f32(3.14/4.)) 72 73print(dsp.arm_sqrt_q31(0x7FFF)) 74 75firf32 = dsp.arm_fir_instance_f32() 76dsp.arm_fir_init_f32(firf32,3,[1.,2,3],[0,0,0,0,0,0,0]) 77print(firf32.numTaps()) 78filtered_x = signal.lfilter([3,2,1.], 1.0, [1,2,3,4,5,1,2,3,4,5]) 79print(filtered_x) 80print(dsp.arm_fir_f32(firf32,[1,2,3,4,5])) 81print(dsp.arm_fir_f32(firf32,[1,2,3,4,5])) 82 83 84def q31sat(x): 85 if x > 0x7FFFFFFF: 86 return(np.int32(0x7FFFFFFF)) 87 elif x < -0x80000000: 88 return(np.int32(0x80000000)) 89 else: 90 return(np.int32(x)) 91 92q31satV=np.vectorize(q31sat) 93 94def toQ31(x): 95 return(q31satV(np.round(x * (1<<31)))) 96 97def q15sat(x): 98 if x > 0x7FFF: 99 return(np.int16(0x7FFF)) 100 elif x < -0x8000: 101 return(np.int16(0x8000)) 102 else: 103 return(np.int16(x)) 104 105q15satV=np.vectorize(q15sat) 106 107def toQ15(x): 108 return(q15satV(np.round(x * (1<<15)))) 109 110def q7sat(x): 111 if x > 0x7F: 112 return(np.int8(0x7F)) 113 elif x < -0x80: 114 return(np.int8(0x80)) 115 else: 116 return(np.int8(x)) 117 118q7satV=np.vectorize(q7sat) 119 120def toQ7(x): 121 return(q7satV(np.round(x * (1<<7)))) 122 123def Q31toF32(x): 124 return(1.0*x / 2**31) 125 126def Q15toF32(x): 127 return(1.0*x / 2**15) 128 129def Q7toF32(x): 130 return(1.0*x / 2**7) 131 132firq31 = dsp.arm_fir_instance_q31() 133x=np.array([1,2,3,4,5])/10.0 134taps=np.array([1,2,3])/10.0 135xQ31=toQ31(x) 136tapsQ31=toQ31(taps) 137dsp.arm_fir_init_q31(firq31,3,tapsQ31,[0,0,0,0,0,0,0]) 138print(firq31.numTaps()) 139resultQ31=dsp.arm_fir_q31(firq31,xQ31) 140result=Q31toF32(resultQ31) 141print(result) 142 143 144a=np.array([[1.,2,3,4],[5,6,7,8],[9,10,11,12]]) 145b=np.array([[1.,2,3,4],[5.1,6,7,8],[9.1,10,11,12]]) 146print(a+b) 147 148print("OK") 149 150v=dsp.arm_mat_add_f32(a,b) 151print(v) 152 153 154 155a=np.array([[1.,2,3,4],[5,6,7,8],[9,10,11,12]]) 156b=np.array([[1.,2,3],[5.1,6,7],[9.1,10,11],[5,8,4]]) 157print(np.dot(a , b)) 158v=dsp.arm_mat_mult_f32(a,b) 159print(v) 160 161def imToReal2D(a): 162 ar=np.zeros(np.array(a.shape) * [1,2]) 163 ar[::,0::2]=a.real 164 ar[::,1::2]=a.imag 165 return(ar) 166 167def realToIm2D(ar): 168 return(ar[::,0::2] + 1j * ar[::,1::2]) 169 170a=np.array([[1. + 2j,3 + 4j],[5 + 6j,7 + 8j],[9 + 10j,11 + 12j]]) 171b=np.array([[1. + 2j, 3 + 5.1j ,6 + 7j],[9.1 + 10j,11 + 5j,8 +4j]]) 172print(np.dot(a , b)) 173 174# Convert complex array to real array for use in CMSIS DSP 175ar = imToReal2D(a) 176br = imToReal2D(b) 177 178v=dsp.arm_mat_cmplx_mult_f32(ar,br) 179print(v) 180 181a=np.array([[1.,2,3,4],[5,6,7,8],[9,10,11,12]]) / 30.0 182b=np.array([[1.,2,3,4],[5.1,6,7,8],[9.1,10,11,12]]) / 30.0 183print(a+b) 184 185aQ31=toQ31(a) 186bQ31=toQ31(b) 187v=dsp.arm_mat_add_q31(aQ31,bQ31) 188rQ31=v[1] 189r=Q31toF32(rQ31) 190print(r) 191 192a=np.array([[1.,2,3,4],[5,6,7,8],[9,10,11,12]]) 193print(np.transpose(a)) 194print(dsp.arm_mat_trans_f32(a)) 195 196a = np.array([[1., 2.], [3., 4.]]) 197print(np.linalg.inv(a)) 198print(dsp.arm_mat_inverse_f32(a)) 199 200a = np.array([[1., 2.], [3., 4.]]) 201print(np.linalg.inv(a)) 202print(dsp.arm_mat_inverse_f64(a)) 203 204a=np.array([[1.,2,3,4],[5,6,7,8],[9,10,11,12]]) 205print(2.5*a) 206print(dsp.arm_mat_scale_f32(a,2.5)) 207 208a=np.array([1.,2,3,4,5,6,7,8,9,10,11,12]) 209print(np.max(a)) 210print(np.argmax(a)) 211print(dsp.arm_max_f32(a)) 212 213print(np.mean(a)) 214print(dsp.arm_mean_f32(a)) 215 216print(np.dot(a,a)) 217print(dsp.arm_power_f32(a)) 218 219 220def imToReal1D(a): 221 ar=np.zeros(np.array(a.shape) * 2) 222 ar[0::2]=a.real 223 ar[1::2]=a.imag 224 return(ar) 225 226def realToIm1D(ar): 227 return(ar[0::2] + 1j * ar[1::2]) 228 229nb = 16 230signal = np.cos(2 * np.pi * np.arange(nb) / nb) 231 232result=np.fft.fft(signal) 233print(result) 234signalR = imToReal1D(signal) 235cfftf32=dsp.arm_cfft_instance_f32() 236status=dsp.arm_cfft_init_f32(cfftf32,nb) 237print(status) 238resultR = dsp.arm_cfft_f32(cfftf32,signalR,0,1) 239resultI = realToIm1D(resultR) 240print(resultI) 241 242signal = signal / 10.0 243result=np.fft.fft(signal) 244print(result) 245 246signalR = imToReal1D(signal) 247signalRQ31=toQ31(signalR) 248cfftq31=dsp.arm_cfft_instance_q31() 249status=dsp.arm_cfft_init_q31(cfftq31,nb) 250print(status) 251resultR = dsp.arm_cfft_q31(cfftq31,signalRQ31,0,1) 252resultI = realToIm1D(Q31toF32(resultR))*16 253print(resultI) 254 255signal = signal / 10.0 256result=np.fft.fft(signal) 257print(result) 258 259signalR = imToReal1D(signal) 260signalRQ15=toQ15(signalR) 261cfftq15=dsp.arm_cfft_instance_q15() 262status=dsp.arm_cfft_init_q15(cfftq15,nb) 263print(status) 264resultR = dsp.arm_cfft_q15(cfftq15,signalRQ15,0,1) 265resultR=Q15toF32(resultR) 266resultI = realToIm1D(resultR)*16 267print(resultI) 268 269 270nb = 128 271signal = np.cos(2 * np.pi * np.arange(nb) / nb) 272signalRQ31=toQ31(signal) 273 274result=np.fft.fft(signal) 275print(result) 276rfftq31=dsp.arm_rfft_instance_q31() 277status=dsp.arm_rfft_init_q31(rfftq31,nb,0,1) 278print(status) 279resultI = dsp.arm_rfft_q31(rfftq31,signalRQ31) 280resultI=Q31toF32(resultI)*(1 << 7) 281print(result) 282 283nb = 128 284signal = np.cos(2 * np.pi * np.arange(nb) / nb) 285signalRQ15=toQ15(signal) 286 287result=np.fft.fft(signal) 288print(result) 289rfftq15=dsp.arm_rfft_instance_q15() 290status=dsp.arm_rfft_init_q15(rfftq15,nb,0,1) 291print(status) 292resultI = dsp.arm_rfft_q15(rfftq15,signalRQ15) 293resultI=Q15toF32(resultI)*(1 << 7) 294print(result) 295 296 297nb = 128 298nb2=64 299signal = np.cos(2 * np.pi * np.arange(nb) / nb) 300result=dct(signal,4,norm='ortho') 301print(result) 302 303signal = signal / 10.0 304result=dct(signal,4,norm='ortho') 305signalQ31=toQ31(signal) 306cfftradix4q31=dsp.arm_cfft_radix4_instance_q31() 307rfftq31=dsp.arm_rfft_instance_q31() 308dct4q31=dsp.arm_dct4_instance_q31() 309status=dsp.arm_dct4_init_q31(dct4q31,rfftq31,cfftradix4q31,nb,nb2,0x10000000) 310print(status) 311state=np.zeros(2*nb) 312resultI = dsp.arm_dct4_q31(dct4q31,state,signalQ31) 313resultI=Q31toF32(resultI)*(1 << 7) 314 315nb = 128 316nb2=64 317signal = np.cos(2 * np.pi * np.arange(nb) / nb) 318signal = signal / 10.0 319result=dct(signal,4,norm='ortho') 320signalQ15=toQ15(signal) 321cfftradix4q15=dsp.arm_cfft_radix4_instance_q15() 322rfftq15=dsp.arm_rfft_instance_q15() 323dct4q15=dsp.arm_dct4_instance_q15() 324status=dsp.arm_dct4_init_q15(dct4q15,rfftq15,cfftradix4q15,nb,nb2,0x1000) 325print(status) 326state=np.zeros(2*nb) 327resultI = dsp.arm_dct4_q15(dct4q15,state,signalQ15) 328resultI=Q15toF32(resultI)*(1 << 7) 329 330 331from pylab import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show 332figure(1) 333plot(np.absolute(signal)) 334t = np.arange(nb) 335freq = np.fft.fftfreq(t.shape[-1]) 336resultmag=np.absolute(result) 337figure(2) 338plot(resultmag) 339figure(3) 340cmsigmag=np.absolute(resultI) 341plot(cmsigmag) 342show() 343 344biquadf32 = dsp.arm_biquad_casd_df1_inst_f32() 345numStages=1 346state=np.zeros(numStages*4) 347coefs=[1.,2,3,4,5] 348dsp.arm_biquad_cascade_df1_init_f32(biquadf32,1,coefs,state) 349print(dsp.arm_biquad_cascade_df1_f32(biquadf32,[1,2,3,4,5]))