Vous êtes sur la page 1sur 15

ELEC4123 Electrical Design Proficiency

Laboratory 2: Signal Processing


/Electronic Systems Design

Group Members: Bryan Loh (z3444283)

Floyd DSouza (z3437815)

Lab day/time/class: Wednesday, 09:00 13:00, Elec Eng 101


Lab Group/Bench Number: 102-C1
Semester/Year: Semester 1, 2015

Aim:
To turn-on and off an LED based on singular (on) or consecutive (off) claps, with a high degree of
reliability, robustness and tolerance to noise.

Introduction
- Detailed Requirements:
This particular design task requires the implementation of an analogue circuit or digital code in
order to correctly process and interpret the signal to fulfil the following:
True requirements:
1. One clap should turn the light on and two claps in quick succession should turn it off
2. The systems operation should be robust and reliable (especially to ambient noise)
3. If using the computer, the sampling rate should be minimised at the input
Desirable Requirements:
1. Larger the distance between the user and microphone that the system can work at
2. Higher robustness to noise

Methods to determine a peak


Envelope detector
An envelope detector is an electronic circuit that takes a high-frequency signal as input and
provides an output, which is the envelope of the original signal. The capacitor in the circuit stores
up charge on the rising edge, and releases it slowly through the resistor when the signal falls.
This technique is very simple and cheap, with low distortion. It is appropriate for this design task
because it provides more definite peaks which can both be detected easier, and characterised by
its shape more efficiently to determine a clap specifically. Before giving the circuit an input, the
signal must be band pass filtered otherwise the circuit will demodulate unnecessary signals.

Windowing
This technique allows segmented parts of the entire value array to be analysed based on the rate
at which the system is receiving information, and how much of it. The technique is particularly
adequate for this project as samples can be taken in real time through a loop in Matlab and
compared to pre and post windows, making the analysis of the time varying signal much simpler.
The window used in this case is a simple rectangular window. The size of each window can be
determined by 2 key characteristics:
- The time between each clap; in this case a size of 0.2s was used (2000 samples), and;
- The maximum number of windows (time taken) for consecutive claps, which was max 3
windows, at approximately 0.6seconds.
The only issue with this technique is that distortion in the signal being analysed, can cause peaks
to overlap and parts of the peak to appear on the edge of one window and the next, incorrectly
identifying 2 peaks in 2 windows when only 1 was found. A Hamming window could have been
used instead to minimise distortion due to its smoother shape, but given the time constraints, was
avoided as it is harder to implement.

Sound Characteristics
The challenge with identifying a clap is that there a many other sounds such as a finger click or a
foot step on a hard surface that results in a high intensity peak in amplitude in the time domain
similar to that of a clap which will be the fundamental shape we will focus on. To be able to
differentiate between a clap, the differences within the actual sound rather than shape of the
sound can be a telling difference. The following differences in sound include:
Pitch: How high or low a sound/note is. Higher up in pitch a particular sound is, higher the
frequency and vice versa lower the pitch, lower the frequency. At a fixed distance, the
pitch of these sounds are quite distinct, however care must be taken when analysing the
frequency of the sound as closer the sound is to the mic, the larger the frequency of the
sound wave due to the Doppler effect, clouding the sound characteristic

Timbre/Tone Colour/Envelope/Spectrum: A sound that distinguishes different types of


sound production such as different types of instruments. In the picture below, the 6
instruments contains the same pitch and amplitude, but very different timbre/envelope
shape to distinguish between them, largely
based around the sounds time variation
and spectral harmonic content in the
frequency domain.
Time Domain
Frequency Domain

Duration: As a clap reverberates a bit more (tens of micro seconds range) than a finger
click, and shorter than a stomp on the ground, this may affect the duration of the voltage
spike1

The only other sound property there is but cant be used in the determination of a clap (from
other similar sounds) is amplitude due to the variability in claps from age, sex, position of clap on
hand etc.

Operation of the light


For this task, we will use a linear current source or open-collector circuit as depicted in
the circuit on the right. When the user claps once, the DAQ output a HI (>0.7V) to the
base of the transistor, in this situation the transistor is now ON, causing current to flow
from Vcc to ground. When the user claps twice, the DAQ outputs zero, this isnt enough
voltage to forward-bias the transistor and the transistor is off causing the LED to be at the
same potential as Vcc as there is no ground connection, therefore no current flows
through the circuit.
As the display (LED circuit) of our signal processing is not as important for this task as the
signal processing coding or circuitry itself, this design will suffice for this task. In practice
and real world application, this driver could be done more efficiently, either using a MOSFET in
accordance with the DAQ to control its gate, with minimal if not no current required, biasing the
MOSFET meaning a very low input power for the necessary output power, therefore high
efficiency system.
Another option available for a real practical application would be using a switch mode current
source which provides near constant current and therefore regulates the voltage using a converter
and automatic adjustment of the duty cycle, to account for the losses and dimming of the LED over
time, a very efficient solution. This circuit is more complex and not necessary for this small task.

Robust solution
The definition of robustness for this project is that with different clapping sequences and
background noises that the system works as it is supposed to. In order to distinctly identify the
signal being fed from the microphone into the DAQ, an amplifying circuit is connected between
the 2, consisting of a resistor and capacitor (takes the output). Within the code itself, a lower pass
filter aims to remove or at least dampen the high frequency components of the ambient noise,
and the side intervals of the peak. Given the resultant noise reduction, a peak can be identified
much more easily by setting a threshold values that defines a peak for all values above, with the
lower the threshold value, the further away the clap can be from the mic and still be detectable.

http://www.animations.physics.unsw.edu.au/jw/sound-pitch-loudness-timbre.htm

Minimise Sampling Rate (Fs)


By minimising the sampling rate, the energy used in the process is effectively decreased as less as
less work is done in taking X amount of samples int seconds. Power conservation is always a
valuable characteristic of a system, especially if the system runs on-going. In order to reduce the
sampling frequency to a sufficient value determined by the designer and still have proper
operation of the circuit, the number of samples taken in per window must therefore be reduced
accordingly, therefore a smaller window. The window size however must be high enough such that
a peak is visible across only 1 window frame and not multiple as it will be harder to differentiate.
As a digital low pass filter is used, the filtering of higher frequencies would allow for a more
smooth signal and thus a lower sampling frequency can be used.

Implementation and Test Plan:


The following table is a program for the project, providing a detailed description of the tasks that need to be completed in order to achieve the
objectives. This will increase productivity and lab efficiency, and ensure project completion within the given time whilst fulfilling the
requirements.
*Note, all formulas and results will be provided in the detailed design section to follow.

Task
No.

Task

Measure
consecutive
claps and
window length

Design Matlab
Low-Pass filter,
test

Implement a
find peaks
algorithm, test

Action
By looking at the response of consecutive claps in Matlab
through the DAQ, determine the time lapse between the
2 claps.
Do tests about 5 times to ensure consistency between
time delay
Find no. of samples in a window (window length) which
allocate 1 peak ONLY in 1 window
Using first principles, implement a Low-Pass filter with the
cut-off frequency (<500Hz)
Using the signal generator, input signals with frequencies
greater than the cut-off frequency and to ensure correct
low pass operation.
Observe the signal in the time domain, looking for the
absolute peaks which correspond to a clap within the
given frequency spectrum. Setting some threshold
minimum value will stop residual noise peaks from being
picked up

Comments
Do the 5 or 6 tests with different people as different people
might perform consecutive claps (naturally) at different speeds.
Looking at the maximum time lapse between gaps, will allow us
to differentiate between singular and consecutive claps.
Never want more than 1 peak in a window therefore must be
small and big enough so that this is the case
No tool box functions can be used in Matlab except for the FFT
required to convert the time domain signal into its frequency
domain.
Low-Pass filter will also make the peaks of the sounds more
accurate with less staggered intervals (high frequency
components) on the way up to the absolute peak
Dont use the windowing technique yet during this stage. Look
at a large set of values first.
This is only the first step of characterising a clap
Note that the size of the threshold value may limit the distance
between the user and the microphone for the system to still

Implement
window, test

Implement logic,
test

Implement
driver circuit,
test

Test a number of sounds, amplitudes and distances away


from the mic and observe their peaks. Determine a good
enough distance where the peak will stand out and set
the threshold value to that (min)
Set window length based on step 1 data.
Set while loop to count to iterate (Fs*Time)/window
length times.
Each iteration (window) must search for a peak governed
by the set threshold value (i.e. just above noise levels)
Identify across how many windows at max, consecutive
(2) claps would go across (x boxes) given the window
length. x number of windows will be used for logic
operations, whereby after a clap, x boxes are checked to
check for another clap.
Given a successful clap;
If another clap is found within the x windows, consider it
consecutive claps and send a LO to be output by the DAQ
to the light
If the x windows find no other peak, consider it a single
clap and send a HI to be output by the DAQ to the light
Test a number of clapping sequences to ensure the
system is robust
If an open collector BJT is used as the LED driving circuit:
If DAQ outputs a HI (>0.7V) into the base of the transistor
If DAQ outputs a LO into the base of the transistor, the
transistor is off (<0.7V) into the base of the transistor
Re-do some of the tests from the plan to ensure correct
operation of the open collector driver circuit.

function properly.

Based on the sampling frequency (Fs) multiplied by the number


of seconds the program will run for, this is the total amount of
samples gathered. The number of windows is
(Fs*Time)/window length.
There is a risks that searching for x windows with (x*window
length) samples could take a long time to process the code,
affecting how fast the output turns on.

This step will require a counter to be established every time an


operation has been undertaken.
Provide multiple test cases that must perform the correct
operation with different clap sequences and times.
Ensure that the HI signal output from the DAQ is > 0.7V and a
low is less than 0.7V to maintain correct operation of the LED
driver circuit analysed next.
Ensure the voltage and current rating of the LED is adhered to
and use sufficient resistor in series with the LED.

Determine
characteristics
of a sound wave
and how they
change the
shape of the
waveform
(OPTIONAL)
Determine the
sound
characteristics
of a clap
(OPTIONAL)

Research the following sound characteristics and its effect on


the sound wave in the time and frequency domain:
- Pitch
- Tone
- Volume/loudness

Research the above characteristics and frequency domain


specifically of a clap.

Implement clap
characterisation
algorithm, test
(OPTIONAL)

Based on the knowledge of a clap, and different sound


wave characteristics of a clap and other similar sounds,
look at the clap enveloped waveform, identifying the clap
characteristics in turns of shape of turning point, gradient,
width etc.
Use this criterion to test for claps

10

Implement
envelope, test
(OPTIONAL)

By using the configuration above, implement envelope


circuit
Test to insure that modulated waveform is smooth and
distinct

These characteristics will form the basis of the envelope or shape of


the clap that we aim to achieve, differing from other sounds similar
in nature such as clicking of the fingers or stomping your foot

Will help determine the frequency band which we will need to


focus on (limits on a band pass filter) to increase robustness
from noise outside this range.
Using the sound characteristics from step 7, specific to a hand
clap, we can form an envelope, defining the characteristics that
will enable us to compare sounds through the microphone to
what we have defined as a clap, to then perform the correct
output operation.
This clap characterisation will be the most difficult to
implement, as claps sound very similar to many other sounds.
There is a risk that the program may spend a lot of time
comparing the actual signal section Can try a matched filter
instead of detail comparison programming
All the operations done in this step is in the time domain

Detailed Design and Solution:


- Clap Detection and Logic
Our logic looks at x number of windows in real-time to determine clap sequence. x is defined by
the minimum number of windows that needs to be looked at sequentially to determine if its 1, 2,
3 etc. claps performed in succession. Setting Fs = 10kHz, and using 5000 samples per window,
when we clapped twice, often both clap peaks were encountered in 1 window, implying the
window was too wide. So decreasing the window size to 2000 samples, then clapping twice at
different speeds, at no stage was there 2 peaks to 1 window, and the 2 claps consistently spread
across either 2 or 3 windows. Therefore we could determine that 3 windows could be used as our
logic test case due to the consistency of clap results, at Fs = 10kHz and 2000 sample window.
These results are shown below of a single and double clap.

Given this, we could implement our logic that was able to distinguish between singles claps and
consecutive (>=2) claps as shown on the next page. The logic accounts for more than 2 consecutive
claps whereby it will keep the light off.

Read in microphone values

Window i'
Restart clap
status
Has a clap been found previously & 2
windows have now passed since then

Yes

Single Clap
Turn light ON

No
No

Clap found
(Window Max value > Threshold)

Yes
Is this the 1st clap found?

Yes

No

Save window number


found = i

Consecutive clap
Turn light OFF
i=i +1

- Filtering
Because of the ambient noise being detected by the microphone alongside the claps, the signal
waveform is very noisy. Therefore, a filter is introduced to process the signal and filter out the
unwanted noise components. The filter implemented in this system is a second-order elliptic low
pass filter. This filter will smoothen out the noisy and unwanted high frequencies of the ambient
noise. The cut-off frequency was chosen at 200Hz as most ambient noise frequencies and its
harmonics were found to be in the kHz range. The filter design was done in MATLAB using
ellipord, ellip and filter functions. The ellipord function takes in the passband and stopband
edge frequencies together with the desired passband ripple and stopband attenuation and returns
a suitable order and elliptic natural frequency for the filter design. The ellip function then takes
these values and returns the filter coefficients. Lastly, the filter function is used to filter the signal
with the designed filter. Below is the comparison between the filtered and unfiltered clap signals:

0.15

0.1

0.05

0.02

0.04

0.06

0.08

0.1

0.12

0.14

0.16

0.18

0.2

Figure 1: Unfiltered clap signal


0.15

0.1

0.05

0.02

0.04

0.06

0.08

0.1

0.12

Figure 2: Filtered clap signal

0.14

0.16

0.18

0.2

As seen, the filtered signal is much smoother compared to the unfiltered one. As such, the clap
peak is more distinct and easily detected. Because of this, the system is able to detect claps at a
much further distance than normal. Also, because the high frequencies have been filtered out, this
allows us to lower our sampling frequency from 15kHz to 10kHz with no effect on the system.

Circuit Design (Hardware)

Figure 3: Clapper circuit design

The circuit design is fairly straightforward for this system as most of the process is done via
software. The electret microphone is first connected in a microphone biasing circuit, which
consists of a resistor (2.2k) and a capacitor (1F). The output of the capacitor is then connected
to the DAQ input for sampling and signal processing. The DAQ interfaces with MATLAB where
most of the filtering and logic is carried out. The DAQ then outputs a logic signal via the output
ports, into the base of the transistor.
When the user claps once, the DAQ output a HI (5V) to the base of the transistor, in this situation
the transistor is now ON, causing current to flow from Vcc to ground. When the user claps twice,
the DAQ outputs zero, this is not enough voltage to forward-bias the transistor and the transistor
is off causing the LED to be at the same potential as Vcc as there is no ground connection,
therefore no current flows through the circuit.

Conclusion and Reflection:


In conclusion, the clapper system achieved the design requirements. The LED switches on when a
clap is detected and OFF when two claps are detected. Secondly, the system is robust to ambient
noise including noises similar in nature to claps such as clicking fingers and tapping the table, as it
has been solved with a second order low-pass filter. Lastly the sampling rate of the system was
minimized to about 10kHz. Moreover, other desirable requirements have also been met, such as
activating the clapper at large distances (8 meters being the largest so far, limited by the room

setup to try further distances), and it is highly robust to noise. Unfortunately, due to time
constraints, we were unable to execute better ideas that would further improve the
responsiveness and the robustness of this system, e.g. envelope detection, clap characterization
through the analysis of sound properties, and the use of a Hamming window instead of a
rectangular window.
This lab had many challenges, which we managed to overcome, and also learned much in the
process. The design task really assessed us on our proficiency in designing, interfacing and
troubleshooting hardware and software and manipulating signals to perform respective actions.
Due to the time constraint and limited resources, we learnt to be more organized and systematic
in laying out a task plan, and executing specific tasks to better achieve our goal. We also learned to
think more critically and analyze a problem more thoroughly, to find a solution and workaround.
Moreover, this lab also taught us to be more particular about everything to the finest detail, as a
small miscalculation could have big impacts on the system (e.g. tuning the low pass filter). Lastly,
we would also like to thank our lecturer Dr. Aboutanios for teaching us to think more critically and
exposing us to real-life working scenarios. Thoroughly enjoyable project.

Appendix A Matlab code


close all; clear all; clc;
info = daqhwinfo('nidaq');
% Predefined values
Fs = 10000; %sampling frequency
found = 0;
off = 0;
seconds = 50; %test duration
i = 0;
samplesPerIteration = 2000;
% Setting up input channels
ai = analoginput('nidaq','Dev2'); %input channels
addchannel(ai,0:2)
set(ai,'SampleRate',Fs) %sampling rate
set(ai,'SamplesPerTrigger',seconds*Fs)
ao = analogoutput('nidaq','Dev2'); %output channels
addchannel(ao,0)
start(ai); %start program
%Low-pass filter
f1 = 200; %Cutoff frequency
f2 = 300; %Stop band
Ap = 0.5; %dB value at f1 (positive difference
As = 20; %dB value at f2 (positive difference
[N,wn] = ellipord(f1/(Fs/2), f2/(Fs/2),Ap,As);
[B1,A1] = ellip(N,Ap,As,wn); % B and A are the

from 0dB - MUST BE POSITIVE VALUE)


from 0dB - MUST BE POSITIVE VALUE)
%N = order, wn = natural frequency
coefficients of the digital filter

samples = seconds*Fs;
totalIterations = samples/samplesPerIteration; %seconds*Fs = total number of smaples
while(i ~= totalIterations)
acquiredData = getdata(ai, samplesPerIteration); %4000 = no. of samples
data1 = acquiredData(:,1); % every sample from channel 1. data1 is of length 4*Fs
y=abs(data1(1)-data1);
%rectified. data1(1) may or may not start at 0, so
find each value relative to the first value
y_lpf = filter(B1,A1,y);
%data after lowpassfilter
t=0:1/Fs:(length(y)-1)/Fs; %time
if (max(y_lpf) > 0.011)
if ((found == 0));
%1st peak in sequence
found = i;
%first peak in 3 box sequence found
if(i == (totalIterations-1)) % account for clap in last window
putsample(ao,5) %output HI
end
else
%next peak found, for consecutive claps
putsample(ao,0)
%turn off LED
off = 1;
found = i;
end
end
if (((i-found)== 2)&&(found~=0)) %Singular clap
if (off == 0)
%reset found after off state change
putsample(ao,5)
end
found = 0;
off = 0;
end

figure(1); plot(t,y_lpf); %real time plot of low-pass filter data


ylim([0 0.15]); xlabel('time'); ylabel('filtered voltage');
i = i+1;
end
putsample(ao,0)

Vous aimerez peut-être aussi