alpha = 0.9 # Koeffizient N_sim = 200 # Anzahl Simulationsschritte x = zeros(N_sim); x[0] = 1.0 # x ist dirac-Stoß mit Gew. 1.0 # Koeffizientenquantisierung: q_coeff = {'QI':0,'QF':3,'quant':'round','ovfl':'wrap'} # Quantisierung nach Summation im Filter: #q_accu = {'QI':0,'QF':4,'quant':'fix','ovfl':'sat'} # keine Grenzzyklen #q_accu = {'Q':1.4,'quant':'fix','ovfl':'wrap'} # große Grenzzyklen bei QI = 0 q_accu = {'QI':0,'QF':4,'quant':'fix','ovfl':'wrap'} # kleine Grenzzyklen # Keine Quantisierung -> Werte für I, F beliebig q_ideal = {'QI':0,'QF':0,'quant':'none','ovfl':'none'} fx_coeff = fx.Fixed(q_coeff) # Fixpoint Object mit Parametern "q_coeff" alpha_q = fx_coeff.fix(alpha) # berechne quantisierten Koeffizienten fx_IIR_id = fx.Fixed(q_ideal) # Fixpoint-Objekt ohne Quantisierung und Overflow fx_IIR = fx.Fixed(q_accu) # Fixpoint-Objekt mit Parametern "q_accu" n = arange(N_sim) t1 = time.time() y = IIR2(fx_IIR_id, x, alpha_q) # ohne Quantisierung yq = IIR2(fx_IIR, x, alpha_q) # #yq = IIR1(fx_IIR, x, alpha_q) t2 = time.time() print('Time = ', t2 - t1) print('Number of overflows = ', fx_IIR.N_over)
channels=n_chan, rate=rate_in, output=True) # Define quantization mode and create a quantization instance for each channel # quantize with just a few bits: q_obj = { 'Q': -2.15, 'quant': 'round', 'ovfl': 'sat' } # try 'quant':'round', 'ovfl':'sat' # Overflows QI = -1 means the MSB is 2^{-1} = 0.5 #q_obj = {'Q':-1.15,'quant':'fix','ovfl':'wrap'} # try 'ovfl':'sat' fx_Q_l = fx.Fixed(q_obj) fx_Q_r = fx.Fixed(q_obj) # initialize arrays for audio samples samples_in = zeros(CHUNK * 2, dtype=np_type) # stereo int16 samples_out = zeros(CHUNK * 2, dtype=float) # stereo float samples_l = samples_r = zeros(CHUNK, dtype=np_type) # separate channels int16 data_out = 'start' while data_out: # read CHUNK stereo samples to string and convert to numpy array. # R / L samples are interleaved, each sample is 16 bit wide (dtype = np.int16) samples_in = np.fromstring(wf.readframes(CHUNK), dtype=np_type)
import matplotlib.pyplot as plt from matplotlib.pyplot import (figure, plot, stem, grid, xlabel, ylabel, subplot, title, clf, xlim, ylim) import sys sys.path.append('..') import dsp_fpga_fix_lib as fx b = [0.01623, 0, -0.06871, 0, 0.30399, 0.5, 0.30399, 0, -0.06871, 0, 0.01623] # q_obj7 = {'QI': 0, 'QF': 7, 'quant': 'floor', 'ovfl': 'none'} q_obj17 = {'QI': 0, 'QF': 17, 'quant': 'floor', 'ovfl': 'none'} # fx_7 = fx.Fixed(q_obj7) fx_17 = fx.Fixed(q_obj17) bq7 = fx_7.fix(b) # quantize a bq17 = fx_17.fix(b) title_str = " b | bq(0.17) | eps(0.17)| bq(0.7) | eps(0.7) " print(title_str, "\n", "-" * len(title_str)) for i in range(len(b)): print("{0:8.5f} | {1:10.6g} | {2:9.2E}| {3:9.6f} | {4:9.2E}".format( b[i], bq17[i], b[i] - bq17[i], bq7[i], b[i] - bq7[i])) w, H_id = sig.freqz(b) w, H_q7 = sig.freqz(bq7) F = w / (2 * pi) fig = figure(1) ax = fig.add_subplot(111)
x = a_sig * sin(2 * pi * t * fsig + 3) # test signal with some starting phase #x = a_sig * sin(2*pi*t*fsig+1) + 0.0001*sin(2*pi*t*2*fsig) #Two-Tone test signal ################################################################## # Distort signal with second and third order non-linearities # and calculate the resulting signal frequencies (with folding) y = x + k2 * x * x - k3 * x**3 N_per = fold(N_per) f_sig_fold = N_per / N_FFT * f_S N_per2 = fold(N_per * 2) f_sig2_fold = N_per2 / N_FFT * f_S N_per3 = fold(N_per * 3) f_sig3_fold = N_per3 / N_FFT * f_S ################################################################## # Define and instantiate quantizer object q_adc = {'QI': 0, 'QF': 13, 'quant': 'round', 'ovfl': 'sat'} adc = fx.Fixed(q_adc) # adc quantizer instance # Quantize input signal (ADC) with or without dither #y += adc.LSB/4. * np.random.rand(len(y)) # add dither, -1/4 LSB < eps_N < 1/4 LSB yq = adc.fix(y) # quantize with the parameters set by q_adc if adc.N_over: print('%d Overflows in ADC: ' % adc.N_over) ################################################################# # # Magnitude of DFT over f = 0 ... f_S/2, scaled mit 2 / N_FFT / sqrt(2) # # This yields correct scaling for: # - SINGLE-SIDED spectra (0 ... f_S/2 ) : factor of 2 # - PERIODIC signalx (factor 1/N_FFT) # - RMS values (factor 1 / sqrt(2) )
NOISE_LABEL = True # add second axis for noise power density NFFT = 2000 f_a = 2 N_per = 7 T_mess = N_per / f_a T_S = T_mess / NFFT t = linspace(0, T_mess, NFFT, endpoint=False) a = 1.1 * sin(2 * pi * f_a * t) # q_obj = { 'QI': 1, 'QF': 3, 'quant': 'round', 'ovfl': 'wrap' } # versuchen Sie auch 'floor' / 'fix' und 'sat' fx_a = fx.Fixed(q_obj) aq = fx_a.fix(a) print('Anzahl der Überläufe = ', fx_a.N_over) # figure(1) title('Quantisiertes Sinussignal und Quantisierungsfehler') plot(t, a, label=r'$a(t)$') plt.step(t, aq, where='post', label=r'$a_Q(t)$') plot(t, a - aq, label=r'$a(t) - a_Q(t)$') plt.legend(fontsize=14) xlabel(r'$t \; \mathrm{/ \; s} \; \rightarrow$') ylabel(r'$a \rightarrow$') A_max = 2**q_obj['QI'] - 2**-q_obj['QF'] plt.axhline(y=A_max, linestyle='--', color='k') plt.axhline(y=-A_max, linestyle='--', color='k')
N = 10000 f_a = 1 t = linspace(0, 1, N, endpoint=False) a = 1.1 * sin(2 * pi * f_a * t) x = linspace(-2, 2, N, endpoint=False) # #q_obj = (0, 4, 'round', 'sat') # try 'round' ; 'sat' q_obj = { 'QI': 0, 'QF': 4, 'quant': 'floor', 'ovfl': 'wrap' } # try 'round' ; 'sat' #q_obj = {'OVFL':'sat'} fx_a = fx.Fixed(q_obj) fx_x = fx.Fixed(q_obj) t_cpu = time.clock() aq = fx_a.fix(a) # quantize a xq = fx_x.fix(x) # quantize x print('Anzahl der Überläufe = ', fx_x.N_over) print('Total CPU time: %.5g ms' % ((time.clock() - t_cpu) * 1000)) # figure(1) title('Quantisiertes Sinussignal') plot(t, a, label=r'$a(t)$') plt.step(t, aq, where='post', label=r'$a_Q(t)$') plot(t, a - aq, label=r'$a(t) - a_Q(t)$')
# Variante 3 # g = [0.16, 1/2.8, 1.4] # a = [1, 0.96] # #b = ones(1,8) #b = sig.remez(10, [0, 0.15, 0.35, 0.5],[1, 0],[1, 1]) # halfband-filter b = [-0.123, 0, 0.379, 0.5, 0.379, 0, -0.123] # halfband-filter b = [0.01623, 0, -0.06871, 0, 0.30399, 0.5, 0.30399, 0, -0.06871, 0, 0.01623] #b = [0.99, 0.] #b=1 #a = array([1,])#,zeros(len(b)-1)] a = 1.0 g = 1.0 ################################################################## # Instanziiere Quantizerobjekte und Fxp-Filter adc = fx.Fixed(q_adc) # adc quantizer instance fil_ma_q = fx.FIX_filt_MA(q_mul, q_acc) # fxpoint filter instance dac = fx.Fixed(q_dac) # dac quantizer instance coeffq = fx.Fixed(q_coeff) ### quantize coefficients aq = coeffq.fix(a) bq = coeffq.fix(b) gq = coeffq.fix(g) if coeffq.N_over > 0: print("Coefficient overflows:", coeffq.N_over) ### Berechnung der Übertragungsfunktion mit idealen und quantisierten Koeff. # idealer Betragsgang [w, Hf] = sig.freqz(b, a, worN=int(N_FFT / 2))
N_per = 53 # Anzahl der Perioden des Signals im DFT-Rechteckfenster # N_per muss kleiner als N_FFT / 2 sein (sonst gibt's Aliasing!) # und am besten eine Primzahl, z.B. 63, 131, 251, 509, 1021, ... # N_per entspricht außerdem der Signalfrequenz! a_sig = 1 # Signalamplitude fsig = f_S * N_per / N_FFT x = a_sig * sin(2 * pi * t * fsig + 1) # + 2^-18 #Testsignal mit Startphase #x = a_sig * sin(2*pi*t*fsig+1) + 0.001*sin(2*pi*t*251) #Two-Tone Testsignal ######################################################################### # Definiere Quantizer-Objekte: # q_adc = {'QI': 1, 'QF': 15, 'quant': 'round', 'ovfl': 'sat'} fx_adc = fx.Fixed(q_adc) # instanstiiere Quantizer ################################################################## ### Input Quantization (ADC, q_adc) if DITHER: ### add uniform dithering noise, -1/4 LSB < eps_N < 1/4 LSB x += 2**(-q_adc['QF'] - 2) * np.random.rand(len(x)) xq = fx_adc.fix(x) # quantisiere if fx_adc.N_over: print('Anzahl der Überläufe = ', fx_adc.N_over) ################################################################# # # FFT über N_FFT Datenpunkte skaliert mit 1/N_FFT über Freq. von 0 ... f_S