330 likes | 347 Views
Learn how to interface and process audio signals in embedded systems to develop a pitch tuner. Capture and amplify the signal, convert to digital, and find the pitch using autocorrelation. Includes hardware and software details.
E N D
Lab descriptions • Lab: Pitch tuner • Lab: Self balancing platform Labs: pitch tuner and self balancing platform (v.5b)
Laboratory 1 A simple Pitch tuner Labs: pitch tuner and self balancing platform (v.5b)
Objectives • To lean how to interface analogue signals to digital systems • To learn how to process audio signals in embedded systems. • Aim • To develop a pitch tuner using an embedded system. Labs: pitch tuner and self balancing platform (v.5b)
Signal processing steps • Capture signal by microphone • Amplify the signal • Feed to ADC of AMR7 micro. • Convert to digital , find autocorrelation coeff. • Find pitch Labs: pitch tuner and self balancing platform (v.5b)
Hardware system RS232 Mic amp ADC input ARM7 PC for display result Mic Labs: pitch tuner and self balancing platform (v.5b)
Amplifier • What is the best bias voltage at pin3(non-invert. input) ? • Condenser MIC output impedance is 75 Ohms, why this circuit is not a good design. How to correct it? • Hint: Input=1KHZ, find impedance of C1 and input of the amp. • Solution: C1>1uF, R2=1K, V3 = 100K(VR), use , one VR to replace VR1,2. Discuss why? Labs: pitch tuner and self balancing platform (v.5b)
Software: Algorithm to find period=1/frequency Labs: pitch tuner and self balancing platform (v.5b)
x n R n Pitch Estimation Method :ACF (Autocorrelation function) • Autocorrelation function (ACF) Symmetrical on both side m Labs: pitch tuner and self balancing platform (v.5b)
What is Auto-correlation, R(m)? • E.g. • x=[1 5 7 1 4 ] • N=5, • R(0)=[x(0)*x(0)+x(1)*x(1)+x(2)*x2+x(3)*x(3)+x(4)*x(4)] • R(0)= (1+ 25+49+1+16)=92 • R(1)=[x(0)*x(1)+x(1)*x(2)+x(2)*x(3)+x(3)*x(4)] • x=[1 5 7 1 4 ] • [1 5 7 1 4 ] • (5+ 35+ 7+ 4)=51 • And so on… • R=[92.0000 51.0000 40.0000 21.0000 4.0000] Labs: pitch tuner and self balancing platform (v.5b)
Example. First, what is auto-correlation? • %matlab code • fs=1 • x=[1 5 7 1 4 8 6 2 4 9 3 ]' • auto_corr_x=xcorr(x) %auto-correlation • figure(1), clf • subplot(2,1,1),plot(x) • grid on, grid(gca,'minor'), hold on • subplot(2,1,2),plot(auto_corr_x) • grid on, grid(gca,'minor') • [pks,locs] = findpeaks(auto_corr_x) • [mm,peak1_ind]=max(pks) • 'peak value1 at location' • pks(peak1_ind) %peak • locs (peak1_ind) %location • 'peak value2 at location' • pks(peak1_ind+1)%peask next to the top peak • locs (peak1_ind+1) %location • period=locs(peak1_ind+1)-locs(peak1_ind) • pitch_Hz=fs/period %display pitch in Hz • %peaks at t=11,15, dt=15-11=4 • Exercise: • Show the steps of calculation X[t] t Auto_correlation(x[t]) R(m) • We only look at positive n • Gap between two peaks is 4, so period of X is around 4 Labs: pitch tuner and self balancing platform (v.5b) Ans: ??
Auto correlation R(j) Rthe_max (j1) Rsecond_max (j2) Lag Time j in samples j1=0 j2 autocorrelation • When a segment of a signal is correlated with itself, the distance (-=Lag_time_in_samples) between the positions of the maximum and the second maximum correlation is defined as the fundamental period (1/pitch_frequency) of the signal. To simplify matter, only the positive j is plotted, -ve j is a just a mirror image. Labs: pitch tuner and self balancing platform (v.5b)
Then the fundamental frequency can be calculated as: • Then the fundamental frequency can be calculated as: • Usually assume j1=0, then Labs: pitch tuner and self balancing platform (v.5b)
Testing a real sound A5_flute 880Hz, (sampling at fs=44100Hz) (x[t]) • %testing a real sound , matlab code • %x=[1 3 7 2 1 9 3 1 8 ], • [xx,fs,nbits]=wavread('c:\sounds\A5_flute.wav'); • sound(x,fs)%fs=44100Hz, • fs %sampling freuqncy • start=10000; %pitch a fram around t=10000 • length=512; • x=xx(start:start+length); • auto_corr_x=xcorr(x); %auto-correlation • figure(1), clf • subplot(2,1,1),plot(x) • title(' one frame of the sound A5-flute=880Hz') • grid on, grid(gca,'minor'), hold on • subplot(2,1,2),plot(auto_corr_x) • title('cross correlation result') • grid on, grid(gca,'minor') • [pks,locs] = findpeaks(auto_corr_x) • [mm,peak1_ind]=max(pks) • 'peak value1 at location' • pks(peak1_ind) %peak • locs (peak1_ind) %location • 'peak value2 at location' • pks(peak1_ind+1)%peask next to the top peak • locs (peak1_ind+1) %location • period=locs(peak1_ind+1)-locs(peak1_ind) • pitch_Hz=fs/period %display pitch in Hz Auto_correlation(x[t]) 2 peaks at t=513, 563 Use sort( ) in matlab to find the two peaks, The gap between 2 peaks is dt=563-513=50, hence frequency is fs/dt=44100/50=882 Hz. Note: Pitch of a flute sound played by a human may not be too stable. Labs: pitch tuner and self balancing platform (v.5b)
Matlab example 1 • a=[1 3 9 3 2 3 8 3 2 ], • r=(xcorr(a)) ,round (r) • R=r(j=time lag) • r=[ 2 9 35 60 96 72 86 123 190 123 86 72 96 60 35 9 17 2] • Exercise • Verify by hand the first 3 (r [ j ]) elements after the signal overlapped with itself. Labs: pitch tuner and self balancing platform (v.5b)
Matlab Example2:a=[1 3 9 3 2 3 8 3 2 ],r=(xcorr(a)) figure(1),clf,subplot(2,1,1)plot(a),subplot(2,1,2)plot(r) Data (a) Period=4, Pick 2 peaks and measure period Auto Corr. Xcorr (r) The middle is the peak Labs: pitch tuner and self balancing platform (v.5b)
Since correlation is mirrored at the lag time (j=0) when the signal overlapped with itself • Only positive time lag is considered • Fundamental Period=j2 Auto correlation R(j) Rthe_max (j1) Rsecond_max (j2) Lag Time j in samples j2 j1=0 Labs: pitch tuner and self balancing platform (v.5b)
Matlab Example3:y=sin wave of 440 Hz • dt=0.0001 • time=[0:dt:2] • freq=440 • nu=2.0*pi*freq • y=sin(nu*time) %angular freq. (sin wave of 440 Hz) • r=(xcorr(y)) • figure(2) , clf, subplot(2,1,1), plot(time,y) • axis ([0 0.01 -1 1]) • subplot(2,1,2), plot(time(1:100),r(1:100)) Labs: pitch tuner and self balancing platform (v.5b)
Matlab Example3:In fact any two peaks will give you the answersine wave of 440 Hz,period=0.0091-0.0068 (by inspection between 2 peaks)freq=1/period=434.7Hz input Data (y) Auto Corr. (r) 0.0068 0.0091 Labs: pitch tuner and self balancing platform (v.5b)
Conclusion • Studied a method of measuring the pitch of an audio sound. Labs: pitch tuner and self balancing platform (v.5b)
Laboratory 2 A self balancing platform Labs: pitch tuner and self balancing platform (v.5b)
Objectives • 1. Objectives • To lean how to interface a direct current (DC) output sensor to an microcontroller • To learn how to implement a Proportional–Integral–Derivative PID feed back control system. • 2. Aim • To develop a self-balancing platform using an embedded system. • Reference: http://www.cse.cuhk.edu.hk/%7Ekhwong/ceg3480/PID_DC_motor_Control08.ppt Labs: pitch tuner and self balancing platform (v.5b)
control method:PID (proportional-integral-derivative) control Required position=0 Integral control I* (deltal) dt error term e=0-tmpl =deltal 0 PWM generator Proportional control =P*(deltal) + - sum tmpl Left_pwm Derivative control D*d(deltal)/dt ; Position Sensor (tmpl) Labs: pitch tuner and self balancing platform (v.5b)
The experimental setup video Labs: pitch tuner and self balancing platform (v.5b)
Fig. 1b.Block Diagram of the Self-balancing platform Labs: pitch tuner and self balancing platform (v.5b)
Fig. 2 Two channels DC amplifier circuit Labs: pitch tuner and self balancing platform (v.5b)
Fig. 3 (a) Top View of the Accelerometer , (b) the sensor attached to the bottom of the platform x-axis y-axis In the arm mcu we set 15000 = read_sensor(1.5V); Labs: pitch tuner and self balancing platform (v.5b)
Special techniques • MIDL is the mid set point value for X-axis • Use integer “int” to simulate floating point • Multiply the value by x and divide it by x afterwards, (e.g. x=200 in our example) • tmpl is the tilt position measurement • e=deltal = (tmpl - (MIDL+200)) / 200; • Derivative is d[e(t)] / dt = e (current ) – e (previous) • diffl = deltal – lastl; • Integration is e dt = e (current) + e (previous) • accul += deltal/200; //arbitrary set to divide by 200, Dead-band (see next page) Labs: pitch tuner and self balancing platform (v.5b)
Dead band • Dead-band : A Dead-band (sometimes called a neutral zone) is an area of a signalrange or band where no action occurs : only enable motor when tile angle outside +/- degrees (=0.1 degrees). http://en.wikipedia.org/wiki/Dead-band Dead-band Labs: pitch tuner and self balancing platform (v.5b)
Algorithm of left motor (core loop in timer IRQ_exception) 500Hz • void __irq IRQ_Exception() • { • tmpl = read_sensor(0); // read X-axis value • //putint(tmpl); // display on terminal • // print("; "); • if (tmpl>=(MIDL+200)) { // if X-axis value >= setpoint plus 200 • deltal = (tmpl - (MIDL+200))/200; // calculate the error and normalize it • diffl = deltal-lastl; // caculate the different between current and last error • if(diffl<maxdiff) { // ignore if the error different > max. difference • // this prevent the noise due to undesired movement of accelerometer • lastl = deltal; // save error as the last error • leftPWM = leftPWM - (P*deltal - I*accul + D*diffl); // update the left PWM value by PID • if (leftPWM<MINOUTPUT) leftPWM = MINOUTPUT; // limit the PWM value to its minimum • if(accul<maxaccu) accul += deltal/200; // ensure the integral not exceed the maximum • PWMMR2=leftPWM; // set the left PWM output • PWMLER = 0x24; //enable match 2,5 latch to effective • } • } • else if (tmpl<=(MIDL-200)) { // if X-axis value <= setpoint plus 200 • deltal = ((MIDL-200) - tmpl)/200; // calculate the error and normalize it • diffl = deltal- lastl; // caculate the different between current and last error • if(diffl<maxdiff) { // ignore if the error different > max. difference • // this prevent the noise due to undesired movement of accelerometer • lastl = deltal; // save error to the last error • leftPWM = leftPWM + P*deltal + I*accul + D*diffl; // update the left PWM value by PID • if (leftPWM>MAXOUTPUT) leftPWM = MAXOUTPUT; // limit the PWM value to its maximum • if(accul>0) accul -= deltal/100; // ensure the integral not less than zero • PWMMR2=leftPWM; // set the left PWM output • PWMLER = 0x24; //enable match 2,5 latch to effective • } • } • //////////////////////////////////////////////////// Labs: pitch tuner and self balancing platform (v.5b)
if (tmpl>=(MIDL+200))Tile position > degrees (outside dead band) • void __irq IRQ_Exception() //running at 500Hz, set by timer • { tmpl = read_sensor(0); // read X-axis value //putint(tmpl); • if (tmpl>=(MIDL+200)) {// if X-axis value >= setpoint plus 200 • deltal = (tmpl - (MIDL+200))/200; // cal.error and normalize it • diffl = deltal-lastl; // cal. different between current and last error • if(diffl<maxdiff) // ignore if the error different > max. difference • {//prevents noise from undesired accelerometer movement lastl = deltal; // save error as the last error • leftPWM = leftPWM - (P*deltal - I*accul +D*diffl);//updatePWM • if (leftPWM<MINOUTPUT) • leftPWM = MINOUTPUT;// limit PWM to its min. • if(accul<maxaccu) //make sure accul cannot grow too big • accul += deltal/200; • // ensure integral< maximum • PWMMR2=leftPWM;// set the left PWM output • PWMLER = 0x24;//enable match 2,5 latch to effective • } • } Labs: pitch tuner and self balancing platform (v.5b)
Else of if (tmpl>=(MIDL+200))Tile position <= degrees (outside dead band) • else if (tmpl<=(MIDL-200)) • { Similar but the rotation is reversed • }//////////////////////////////////////////////////// Labs: pitch tuner and self balancing platform (v.5b)
Question • Is the output voltage changes linearly with the tilt angle? • What is the effect on the performance of the system when the sampling frequency is 100Hz? • The deadband in sensor reading is +/-200. Estimate the deadband in degrees of our system. • What is the effect on the performance of the system when the sampling frequency is 1000Hz? Explain your observation. Labs: pitch tuner and self balancing platform (v.5b)
Conclusion • Studied how to interface direct currents sensor to a embedded system • Studied how to build and tune a PID control system Labs: pitch tuner and self balancing platform (v.5b)