DSP Final Report
DSP Final Report
Final Report
C ONTENTS
I Introduction 1
II    Theory                                                                                                                                                                                 1
      II-A   Delay and Sum Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                                1
      II-B   Microphone Array Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                                 1
             II-B1     Microphone spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                                1
III   Simulation                                                                                                                                                                             2
      III-A   Source localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                            2
      III-B   Spatial Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                          3
IV System Requirements 3
V     Design                                                                                                                                                                                 4
      V-A    Hardware . . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    4
      V-B    DSP Software . . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    4
      V-C    Interface Software . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    5
             V-C1       DSK-PC Interface        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    5
             V-C2       Interface GUI . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    5
VI    Test Methods                                                                                                                                                                           6
      VI-A    Source localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                            6
      VI-B    Spatial filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                          6
VII Results 6
VIII Discussion                                                                                                                                                                              6
     VIII-A Sources of difficulty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                            6
IX Conclusion 7
References 7
                    I. I NTRODUCTION
   Most broadly, beamforming is the use of an array
of antennas - or in the case of audio, microphones
- to perform signal processing based on the spatial
characteristics of a signal. We will discuss two primary
forms of beamforming in this document: source localiza-
tion and spatial filtering. Source localization attempts to
determine the location in space a signal originated from,        Figure 1.   Planar wavefront approaching a linear microphone array.
while spatial filtering creates an electronically-steerable
narrow-beam antenna, which has gain in one direction
and strong attenuation in others. Spatial filtering systems      added. This essentially creates a spatial filter, which we
and corresponding transmission techniques can replace            can point in any direction by changing the delays.
physically moving antennas, such as those used for radar.          To determine the direction a signal came from, we can
   Acoustic source localization is familiar to all of us:        sweep our beam around the room, and record the total
we have two ears, and by using them together, our brain          power of the signal recieved for each beam. The source
can tell where sounds come from. Similarly, our brains           came from the direction with the highest-power signal.
are able to focus on one particular sound and tune out
the rest, even when the surrounding noise is much louder         B. Microphone Array Design
than the sound we are trying to hear.
                                                                    Microphone arrays can be nearly any shape: linear, cir-
                                                                 cular, rectangular, or even spherical. A one-dimensional
                        II. T HEORY
                                                                 array allows beamforming in one dimension; additional
A. Delay and Sum Method                                          array dimensions allow for 2-dimensional beamforming.
   If we have an array of microphones and sufficient             Given the limited number of microphones and amount
signal-processing capability, we can measure the time            of time we have, a linear array is the best choice.
delay from the time the sound strikes the first micro-              1) Microphone spacing: The spacing of the micro-
phone until it strikes the second. If we assume waves            phones is driven by the intended operating frequency
originate far enough away that we can treat the edge of          range.
its propagation as a plane, then the delays are simple to           For spatial filtering, a narrower beam width is an
model with trigonometry as shown in Figure 1.                    advantage, because signals which are not directly from
   Suppose we have a linear array of microphones, m1             the intended direction are attenuated. A narrow beam
through mn , each spaced Dmic meters apart. Then                 width is analogous to a narrow transistion band for
Ddelay , the extra distance the sound has to travel for          a tranditional filter. Lower frequencies will correlate
each successive microphone, is given by                          better with delayed versions of themselves than high
                                                                 frequencies, so the lower the frequency, the broader the
                 Ddelay = Dmic · cos (θ)                  (1)    beam. Conversely, a longer array will result in a greater
At sea level, the speed of sound is 340.29 ms , which            delay between the end microphones, and will thus reduce
means that the time delay is                                     the beam width.
                                                                    At the same time, the spacing between microphones
                            Dmic
               Tdelay =             · cos (θ)             (2)    determines the highest operating frequency. If the wave-
                          340.29 ms                              length of the incoming signal is less than the spacing
By reversing this delay for each microphone and sum-             between the microphones, then spatial aliasing occurs.
ming the inputs, we can recover the original signal. If a        An example is shown in Figure 2.
signal comes from a different direction, the delays will            The spacing between microphones causes a maximum
be different, and as a result, the individual signals will       time delay which, together with the sampling frequency,
not line up and will tend to cancel each other when              limits the number of unique beams that can be made.
                                                                                                                                            2
                                    90                                                                      90
                                             6
                          120                    60                                                 120               60
                    150                                30                                     150                             30
                                         2
180 0 180 0
Figure 2. Spatial aliasing. The source is 1 kHz, located at 135 degrees.   Figure 3. Source localization simulation using a 400 Hz source and
An image appears at approximately 50 degrees, which means that             a 4-microphone array. The source was located at 0, 45, 90, 135, and
sound coming from that direction is indistinguishable from the desired     180 degrees.
source.
                                                                                                            90
The maximum number of beams, maxbeams , is shown                                                    120               60
mathematically in Equation 3.
                                                                                              150                             30
              maxbeams = 2 · Fs · timespacing                       (3)
The variable timespacing is the maximum amount of
time it takes for sound to travel from one microphone to                                180                                         0
A. Source localization
                                                                                              150                             30
   In the source localization code, a single source is
located 10 meters away from the center of the array.
A loop creates thirty beams which cover a 180-degree                                    180                                         0
range, and the power for each beam is computed. Figure
3 shows the power sweeps for five different source
angles. Note that in each case, the maximum power is                                          210                             330
exactly at the angle the sound originates from.
   Using the beam-sweeping technique, we examined the                                               240               300
relative effects of the number of microphones and the                                                       270
                                         60                                                                    −10
                    150                                  30
20 −20
              180                                              0
                                                                                                               −30
                                                                                                               −40
                    210                                  330
                                                                                                               −50
                          240                      300
                                  270
                                                                                                               −60
                                                                                                                     0   100   200        300       400           500   600   700
                                                                                                                                          Frequency (Hz)
Figure 6. Decibel-scale plot of a 4-microphone array beampattern for    Figure 7. The noise source at 300Hz is attenuated by about 3dB using
a 600 Hz signal at 90 degrees. Note the relatively large sidelobes on   an array of two microphones with sources separated by 60 degrees.
both sides of the main beam.
                                                                                                               −15
lower sidelobes in exchange for a wider transition band.
However, this windowing is unlikely to be useful for our                                                       −20
                                                                                                               −35
B. Spatial Filtering                                                                                           −40
−5
                                       −10
                                                                                                            Figure 12.   Flowchart for the main DSP software.
   Relative magnitude of signal (dB)
−15
                                       −20
                                                                                                            cm apart. Theses microphones will be connected to the
                                       −25
                                                                                                            microphone inputs on the DSK_AUDIO4 daughtercard.
                                       −30                                                                  The DSK will be programmed to process a 256 sample
                                       −35                                                                  block at a time. The software delays each microphone’s
                                       −40                                                                  input by a different number of samples then adds each
                                       −45                                                                  input to create the output. In source localization mode,
                                       −50
                                                                                                            the power of the output is calculated and sent to the PC
                                             0   100   200        300       400           500   600   700
                                                                  Frequency (Hz)                            GUI. In spatial filtering mode, the output is passed to
                                                                                                            one of the board’s output channels.
Figure 10. With an array of four microphones with sources separated
by 90 degrees the noise signal is about 12dB down from the signal we
attempted to isolate.                                                                                       A. Hardware
                                                                                                              In addition to the standard DSK board provided to
                                                                                                            the DSP class we will be using the Educational DSP
  •                                    The system must have a GUI running on a computer
                                                                                                            DSK_AUDIO4 daughtercard, which has four input chan-
                                       which communicates with the DSK board.
                                                                                                            nels. Each of these channels will be connected to one of
  •                                    In localization mode:
                                                                                                            the generic mono microphones provided to the class by
                                         – The system will be able to locate the direction                  Dr. Waldo. The placement of the microphones introduces
                                            of a 400 Hz tone between -90 and +90 degrees,                   a physical variable to the system that is important to the
                                            with +/- 5 degrees error. This target tone will                 operation of hardware: according to our specifications
                                            be significantly above the noise threshold in the               and simulations the microphones should be spaced 25
                                            room.                                                           cm apart. Ideally, they will face the direction of incoming
                                         – The GUI must display the direction of the                        acoustic sources for maximum input power.
                                            incoming sound with a latency less than 1
                                            second.
  •                                    In directional enhancement mode:                                     B. DSP Software
                                         – The GUI must allow the user to select a partic-                     The foundation of our DSP software is the sample
                                            ular direction to listen from, between -90 and                  code provided by Educational DSP and modified by Dr.
                                            +90 degrees, and with a resolution of at least                  Waldo. This code initializes the DSP and the codec and
                                            10 degrees.                                                     calls a processing function that provides samples from
                                         – The system should play the selected directional                  all four input channels. Using this as a base we will
                                            output via one of the line out channels.                        implement our main function inside of the processing
                                         – A 300 Hz noise source with the same power                        function provided. In this main file we will also include
                                            will be placed 60 degrees apart from the                        a file that defines our sum and delay functions which
                                            desired signal source. The directional output                   will exist in their own files.
                                            should give a signal-to-noise ratio of at least                    The main software branch opens a RTDX channel with
                                            3 dB.                                                           the PC GUI and keeps track of the current operating
  •                                    The system will operate properly when the sound                      mode. The mode is a boolean value where a TRUE
                                       sources are at least 4 meters away from the micro-                   means spatial filtering mode and a FALSE means source
                                       phone array.                                                         localization mode. In the spatial aliasing mode we do
                                                                                                            a simple delay and sum of the input data. The delay is
                                                                                                            given by the GUI. In localization mode the current output
                                                               V. D ESIGN                                   is also the delay and sum of the input; however, the delay
   The acoustic beam forming system consists of three                                                       sweeps from a minimum possible delay to a maximum
high level subsystems: the microphone array, the DSK                                                        possible delay defined by the number of samples it takes
board, and a GUI running on a PC. A block diagram                                                           to go from −90o to 90o . After every delay and sum
showing the interconnections of these subsystems is                                                         operation the delay/power pairs will be sent to the GUI
shown in Figure 11. The microphone array should con-                                                        for display via the RTDX channel. The flow of this is
tain two to four microphones placed in a straight line 25                                                   shown in Figure 12.
                                                                                                                                      5
                                                                                                    Table I
Figure 13. Flowchart for the function that calculates the number of              B YTE SIGNALS FOR PC→DSK COMMUNICATION
samples to delay each microphone’s input by.
                                                                                          Use              Code       Range
Figure 14. Flowchart for the DSK function that calculates the current            Microphone time spacing              0-100
output sample.                                                                   Number of microphones                  2-4
                                                                                         Mode                           1,2
                                                                                     Beam selection                -100 to +100
   To determine how much to delay each microphone by
we get the number of samples each microphone should
be delayed by from the GUI and multiply by the order
of that microphone in the array. The first microphone in
the array is defined as microphone 0 and has a 0 delay.                 Figure 16.   Byte transmission order for PC → DSK communication
If the delay we get from the GUI is negative the order is
reversed so that the earliest input always has a 0 delay.
The calcDelay function will return a pointed to an array                    We found that we did not need any synchronization
called delays. Figure 13 shows the structure and flow for               bytes, and we trimmed the communictation down to a
calcDelay.                                                              single byte for the power paired with a single byte for
   The sum function accepts the current four input sam-                 delay. This was intended to speed up RTDX communi-
ples and delay from the GUI and passes it to calcDelays.                cation, but was not successful.
Figure 14 shows a program flow for this function. It                          b) Settings to DSK Board: The GUI will control
will have a reverse index tracked buffer so that if                     several settings, summarized in Table I Physical variables
i=5 is the current input i=6 is the oldest input and                    such as microphone spacing and number of microphones
i=4 is the second most recent input. This requires                      need to be easily changed by software in real time, and
fewer rollover-error-checking comparisons relative to a                 the GUI provides an interface for users to the DSK to
forward-tracking buffer, which should give us an incre-                 change the operation as required.
mental speed improvement.                                                   The byte stream will follow the same format as the
                                                                        DSK to PC stream. Data will be sent in 3 byte packets
C. Interface Software                                                   as shown in Figure 16. Packets will only be sent when
                                                                        there is new data, so we expect the number of packets
   1) DSK-PC Interface: The PC will communicate with                    per second to be very low.
the DSK board using RTDX 2.0. For speed, the DSK                            We did not implement this due to time constraints.
will only perform integer operations; thus, the PC will
                                                                            2) Interface GUI: The interface software will be
perform the floating-point work of translating delays
                                                                        written in Java. An overview of the main classes is shown
to angles and vice-versa. There will be two RTDX IO
                                                                        in Figure 17.
channels, one going each direction.
                                                                            The Beamformer class handles the low-level transla-
     a) Power-vs-Delay to PC:
                                                                        tion and communication with the DSK board. It converts
   • Delay will be a 2’s complement 8 bit signed num-
                                                                        from sample delays to angles and vice-versa, converts
     ber, which represents the relative delay between                   booleans to code values, and floating-point values to
     microphones. This will be calculated from the mi-                  integers for high-speed communication.
     crophone spacing and the speed of sound.                               The BeamDisplay is a GUI class which displays the
   • Power will be a 16-bit unsigned number, which
                                                                        beam power levels and sound direction on a polar plot,
     holds the relative power of the signal for a particular            similar to the simulation plots. It will use the Java
     beam.                                                              AWT/Swing toolkit and associated drawing classes to
In order for these two values to remain correlated, we                  implement a custom drawable canvas. Clicking on the
will interleave both on one channel, and signal the start               display will select the direction to use when in spatial
of each new delay-power pair using the value 0xFF. This                 filtering mode.
will allow our system to recover from lost or delayed                       The Settings panel will contain a series of standard
bytes.                                                                  Swing components to select the operating mode, number
   This data will only be sent when the system is in                    of microphones, microphone spacing, the number of
source localization mode.                                               beams per sweep. It will have a pointer to the Beam-
                                                                        former object, so it can configure it directly.
                                                                            The MainWindow class creates the main GUI window,
                                                                        the display and control panels, and the connection to
                                                                        the DSK board. Once running, it will regularly poll
Figure 15.   Byte transmission order for DSK→ PC communication          the beamformer class for new data, and update the
                                                                                                                       6
                               MainWindow
                                                                 We were unable to get any kind of beamforming
                        +guiComponents
                        -connectToDSK()
                                                              to work with four microphones. After getting the 2-
                        -initializeGui()                      microphone system working, we switched to 4 by chang-
                        -disconnect()
                                                              ing a #define in our code and connecting two addi-
                                             ControlPanel
                                                              tional microphones to the DSK expansion board. When
               BeamDisplay             +modeSelect
                                                              we ran the GUI, we were unable to observe any kind of
             +canvas: JPanel           +numBeams              localization.
                                       +numMicrophones
             -initialize()
                                       +microphoneSpacing        Each adjacent pair of microphones worked in a 2-
             +update(newData)
                                       -initialize()          microphone beamformer, so we believe that it was not
                                                              a simple case of one microphone not working properly
                                Beamformer                    or a result of mixing up the order of the microphones.
             -ControlChannel: RTDX Channel                    We performed a test where we merely averaged the
             -DataChannel: RTDX Channel
             +setMode(mode:int)
                                                              inputs from the four microphones and sent that value
             +setNumBeams(newNum:int)                         to the output, which is equivalent to forming a beam
             +setNumMicrophones(newNum:int)
             +setMicrophoneSpacing(newSpacing:double)         perpendicular to the array. Using one of the speakers
             +setFilterAngle(newAngle:int)
             +update()
                                                              as an audio source and measuring the RMS voltage of
             +hasNewData()                                    the output on the oscilloscope, we manually checked the
             +getBeamPower()
                                                              directionality of the array. We were unable to measure
                                                              any significant difference between the sound source be-
Figure 17.   Class Diagram for the GUI
                                                              ing directly orthogonal to the array and at a small angle
                                                              to it. This indicates that at least part of the problem is
                                                              not due to our software. There may have been additional
BeamDisplay accordingly.
                                                              problems with our software for four microphones, but
                                                              because we were unable to get this simple test working,
                      VI. T EST M ETHODS                      we did not test our software further.
A. Source localization
   To test performance of the source localization method                        VIII. D ISCUSSION
we set up two microphones 25cm apart. Using a meter           A. Sources of difficulty
stick to keep a contsant radius, an amplified speaker was
                                                                 We identified several factors which contributed to our
swept radially across the plane level with the micro-
                                                              system’s failure to meet its requirements.
phones. A digital oscilloscope was used to measure the
RMS voltage of the processed output. We also observed            First, there were several acoustic problems. We found
the beam displayed on the GUI.                                that the supplied microphones are very directional -
                                                              they are specifically labeled “unidirectional”. Because
                                                              the beamforming system assumes omnidirectional input,
B. Spatial filtering                                          unidirectional microphones will cause oblique signals to
   Because of time constraints and unexpected difficul-       be attenuated. This is fine when the desired beam is per-
ties with other parts of the project, we did not explicitly   pendicular to the array, but is suddenly a problem when
test spatial filtering.                                       the goal is to amplify oblique signals over orthogonal
   To test the spatial filtering mode, we would have set      ones.
up two sources at for different frequencies equidistant          The audio input level from the microphones seemed
from the array with a 90 degree arc length seperation.        to be rather low, producing averaged output values in
The DSK would process the data with a beam formed             the single-millivolt range. We are not sure if there is a
towards one of the sources and feed the output was to         way to configure additional gain on the DSK board, or
a digital oscilloscope or MATLAB/Simulink GUI set to          if using different microphones would help. Due to the
view the FFT. Using this method, we could compare the         low input level, we had to play the signal very loudly on
relative strengths of the signals.                            the speakers and keep them within one or two meters of
                                                              the array.
                                                                 A 1-meter source distance clearly violates the farfield
                          VII. R ESULTS                       plane-wave assumption - our initial specification as-
   Our system is able to do beamforming with 2 mi-            sumed that 4 meters was farfield. This may have caused
crophones, and display the result on a PC GUI. By             some problems, but re-running our MATLAB simulation
observation, the beamwidth is about what we expected          using a source only 1m away produced a beam virtually
for a 2-microphone array.                                     indistinguishable from a 10-meter beam.
                                                                                                                                        7
   We also experienced very strong echoes in the room,                  [8] R. J. Lustberg, “Acoustic Beamforming Using Microphone Ar-
because of its cement construction and solid wood                           rays,” Master’s thesis, Massachusetts Institute of Technology,
                                                                            Cambridge, MA, June 1993.
lab benches. Working in an environment with reduced                     [9] N. Mitianoudis and M. E. Davies, “Using Beamforming in the
echoes would probably have yielded better results. This                     Audio Source Separation Problem,” in 7th Int Symp on Signal
was evident during late nights in the lab when people                       Processing and its Applications, no. 2, 2003.
working in the ASAP room and janitorial staff would
peek into the room to find the very loud noise echoing
throughout the HSH.
   On the software side, we experienced difficulties get-
ting the bandwidth we desired over RTDX. I had also
seen poor RTDX performance before, when sending data
from a PC to the DSK during the cosine generation lab.
There are several possible causes of this: One is that
we only send a few bytes at a time, but do it dozens
of times per second. Recommendations we found online
suggested that filling the RTDX queue and sending large
packets of data is the fastest way to send data.
   Another likely cause is our DSK boards. We read a
mailing list correspondence between someone who is
familiar with this family of DSKs and a hapless hobbyist
having similar issues. The “expert” claimed that there
is obfuscation hardware on the DSK boards introduced
by Spectrum Digital that slows down RTDX communi-
cation. It was mentioned that a possible workaround is
using the JTAG connector on the DSK board, but the
hardware required for this is not readily available (and
the JTAG port is blocked by the DSP_AUDIO_4). We
looked at high-speed RTDX, but it is not supported on
this platform.
                   IX. C ONCLUSION
   Although we did not meet our initial specifications,
our project was still somewhat successful. We were able
to implement a working beamforming system, and most
of the problems we encountered were limitations of our
equiment which were not under our control.
                          R EFERENCES
[1] M. Brandstein and D. Ward, eds., Microphone Arrays: Signal Pro-
    cessing Techniques and Applications. Berlin, Germany: Springer,
    2001.
[2] J. Benesty, J. Chen, and Y. Huang, Microphone Array Signal
    Processing. Berlin, Germany: Springer, 2008.
[3] J. Chen, L. Yip, J. Elson, H. Wang, D. Maniezzo, R. Hudson,
    K. Yao, and D. Estrin, “Coherent Acoustic Array Processing and
    Localization on Wireless Sensor Networks,” Proceedings of the
    IEEE, vol. 91, no. 8, pp. 1154–1162, 2003.
[4] J. C. Chen, K. Yao, and R. E. Hudson, “Acoustic Source Localiza-
    tion and Beamforming: Theory and Practice,” EURASIP Journal
    on Applied Signal Processing, pp. 359–370, 2003.
[5] T. Haynes, “A primer on digital beamforming,” Spectrum Signal
    Processing, 1998.
[6] W. Herbordt and W. Kellermann, “Adaptive Beamforming for Au-
    dio Signal Acquisition,” Adaptive Signal Processing: Applications
    to Real-World Problems, pp. 155–196, 2003.
[7] R. A. Kennedy, T. D. Abhayapala, and D. B. Ward, “Broadband
    nearfield beamforming using a radial beampattern transformation,”
    IEEE Transactions on Signal Processing, vol. 46, no. 8, pp. 2147–
    2156, 1998.
                                                                                    8
                                         A PPENDIX A
                        MATLAB CODE FOR BEAM - SWEEP SOURCE LOCALIZATION
 1   % General setup
 2   clear;
 3
 4   % Speed of sound at sea level
 5   speedOfSound = 340.29; % m/s
 6
 7   % Source setup
 8   % Frequency of the audio source
 9   sourceFreq = 700; % Hz
10
11   % Just so we know - since this will affect our mic placement
12   wavelength = speedOfSound / sourceFreq;
13
14   % xy location of audio source
15   sourceLocX = 0; % in meters
16   sourceLocY = 10;
17
18   % Microphone setup
19   numMicrophones = 4;
20
21   % Distance between microphones - determines the highest frequency we can
22   % sample without spatial aliasing
23   micSpacing = 0.25; % meters
24
25   % Minimum amount of time it takes for sound to travel from one mic to the next
26   timeSpacing = micSpacing/speedOfSound;
27
28   % Total width of the array - determines the lowest frequency we can
29   % accurately locate
30   arrayWidth = (numMicrophones - 1) * micSpacing;
31
32   % xy locations of the microphones
33   micLocX = linspace(-arrayWidth/2, arrayWidth/2, numMicrophones); % in meters
34   micLocY = zeros(1, numMicrophones);
35
36
37   % Sampling rate
38   Fs = 44100; % Hz
39
40
41   % Distance from the source to the mic
42   propDistance = hypot(sourceLocX - micLocX, sourceLocY - micLocY);
43
44   timeDelay = propDistance/speedOfSound;
45
46
47   % Create some of the signal
48   soundTime = 0:1/Fs:.125;
49
50   sourceSignal = sin(2 * pi * sourceFreq * soundTime);
51
                                                                                   9
                                      A PPENDIX B
                             MATLAB CODE FOR SPATIAL FILTERING
 1
 2
 3   % General setup
 4   clear;
 5
 6   % The frequencies we are interested in are:
 7   % Right now this is optimized for sources between 100 and 500 Hz
 8   % Change this to whatever range you are interested in.
 9   fmin = 00; % Hz
10   fmax = 700;% Hz
11
12   % Speed of sound at sea level
13   speedOfSound = 340.29; % m/s
14
15   % Source setup
16   % Frequency of the audio sources
17   source1Freq = 300; % Hz
18   source2Freq = 400; % Hz
19
20   % Just so we know - since this will affect our mic placement
21   wavelength1 = speedOfSound / source1Freq;
22   wavelength2 = speedOfSound / source2Freq;
23
24   degreesApart=60;
25   % xy location of audio source
26   source1LocX = sqrt(10^2 + 10^2)*cosd(45+degreesApart); % in meters
27   source1LocY = sqrt((10^2 + 10^2)-source1LocX^2);
28   source2LocX = 10; % Secondary source
29   source2LocY = 10;
30
31   % Microphone setup
32   numMicrophones = 2;
33
34   % Distance between microphones - determines the highest frequency we can
35   % sample without spatial aliasing
36   micSpacing = 0.25; % meters
37
38   % Minimum amount of time it takes for sound to travel from one mic to the next
39   timeSpacing = micSpacing/speedOfSound;
40
41   % Total width of the array - determines the lowest frequency we can
42   % accurately locate
43   arrayWidth = (numMicrophones - 1) * micSpacing;
44
45   % xy locations of the microphones
46   micLocX = linspace(-arrayWidth/2, arrayWidth/2, numMicrophones); % in meters
47   micLocY = zeros(1, numMicrophones);
48
49
50   % Sampling rate
51   Fs = 96000; % Hz
                                                                                     12
 52
 53
 54   % Distance from the source to the mic
 55   prop1Distance = hypot(source1LocX - micLocX, source1LocY - micLocY);
 56   prop2Distance = hypot(source2LocX - micLocX, source2LocY - micLocY);
 57
 58   time1Delay = prop1Distance/speedOfSound;
 59   time2Delay = prop2Distance/speedOfSound;
 60
 61   % Create some of the signal
 62   soundTime = 0:1/Fs:.125;
 63
 64   source1Signal = sin(2 * pi * source1Freq * soundTime);
 65   source2Signal = sin(2 * pi * source2Freq * soundTime);
 66
 67   % Delay it by the propagation delay for each mic
 68   for ii = 1:numMicrophones
 69     received(ii,:) = delay(source1Signal, time1Delay(ii), Fs)...
 70         + delay(source2Signal, time2Delay(ii), Fs);
 71   end
 72
 73
 74   % Direct the beam towards a location of interest
 75   angleWanted = 45; % Degrees (for simplicity)
 76   angleToDelay = angleWanted * pi/180; % Convert to radian
 77
 78   % We want to take the fft of the signal only after everymicrophone is
 79   % getting all of the data from all sources
 80   deadAirTime = (max([time1Delay, time2Delay]));
 81   deadAirSamples = deadAirTime * Fs;
 82   endOfCapture = length(received(1,:));
 83
 84   % Start off with an empty matrix
 85   formedBeam = zeros(1, max(length(received)));
 86
 87   % For each microphone add together the sound received
 88   for jj = 1:numMicrophones
 89       formedBeam = formedBeam + ...
 90           delay(received(jj,:), + timeSpacing*sin(angleToDelay) * (jj-1), Fs);
 91   end
 92
 93   % Get the PSD object using a modified covariance
 94   beamPSD = psd(spectrum.mcov, formedBeam,’Fs’,44100);
 95   % Get the magnitude of the PSD
 96   formedSpectrum = abs(fft(formedBeam));
 97   % The fft sample # needs to be scaled to get frequency
 98   fftScaleFactor = Fs/numel(formedSpectrum);
 99
100   % The frequencies we are interested in are:
101   % Right now this is optimized for sources between 100 and 500 Hz
102   %fmin = 0; % Hz
103   %fmax = 600;% Hz
104
105   % Plot the PSD of the received signal
                                                                                13
106   figure(1);
107   % Get the frequencies and data out of the PSD object
108   beamPSDfreqs=beamPSD.Frequencies;
109   beamPSDdata = beamPSD.Data;
110   % Get the indeces that correspond to frequencies specified above
111   indexesToPlot=find(beamPSDfreqs>fmin-1):find(beamPSDfreqs>fmax,1);
112   % Actually plot it (in a log10 scale so we have dB)
113   plot(beamPSDfreqs(indexesToPlot),20*log10(beamPSDdata(indexesToPlot)));
114   title(’PSD using a modified covariance of the received signal’);
115   ylabel(’Power/Frequency (dB)’);
116   xlabel(’Frequency (Hz)’);
117
118   % Plot the fft of our received signals
119   figure(2);
120   maxLimit=round(fmax/fftScaleFactor);
121   minLimit=round(fmin/fftScaleFactor);
122   if minLimit <= 0
123       minLimit = 1;
124   end
125   f=linspace(0,44100,44100/fftScaleFactor);
126
127   % This gets all of the fft frequencies we want to look at
128   fOfInterest = f(minLimit:maxLimit);
129   % Grab the portion of the fft we want to look at
130   spectrumOfInterest = formedSpectrum(minLimit:maxLimit);
131   % Normalize this so that the max amplitude is at 0db.
132   spectrumOfInterest = spectrumOfInterest/max(formedSpectrum);
133   % Plot it
134   plot(fOfInterest,20*log10(spectrumOfInterest));
135   title(’FFT of the received signal’);
136   ylabel(’Relative magnitude of signal (dB)’);
137   xlabel(’Frequency (Hz)’);
                                                                                14
                                       A PPENDIX C
                             C CODE FOR MAIN PROCESSING LOOP
 1
 2 void processing(){
 3   Int16 twoMicSamples[2];
 4
 5   Int32 totalPower; // Total power for the data block, sent to the PC
 6   Int16 tempPower; // Value used to hold the value to be squared and added
 7   Int16 printCount; // Used to keep track of the number of loops since we last sent data
 8   unsigned char powerToSend;
 9
10   Int8 firstLocalizationLoop; // Whether this is our first loop in localization mode
11   Int8 delay; // Delay amount for localization mode
12
13          QUE_McBSP_Msg tx_msg,rx_msg;
14          int i = 0; // Used to iterate through samples
15
16   RTDX_enableOutput( &dataChan );
17
18          while(1){
19
20                  SEM_pend(&SEM_McBSP_RX,SYS_FOREVER);
21
22                  // Get the input data array and output array
23                  tx_msg = QUE_get(&QUE_McBSP_Free);
24                  rx_msg = QUE_get(&QUE_McBSP_RX);
25
26     // Spatial filter mode
27
28     // Localization mode
29     if(firstLocalizationLoop){
30        delay = DELAYMIN;
31        firstLocalizationLoop = FALSE;
32     }
33     else{
34        delay++;
35        if(delay > DELAYMAX){
36          delay = DELAYMIN;
37        }
38     }
39
40                  // Process the data here
41     /* MIC1R data[0]
42      * MIC1L data[1]
43      * MIC0R data[2]
44      * MIC0L data[3]
45      *
46      * OUT1 Right - data[0]
47      * OUT1 Left - data[1]
48      * OUT0 Right - data[2]
49      * OUT0 Left - data[3]
50      */
51
                                                           15
52   totalPower = 0;
53
54               for(i=0; i<QUE_McBSP_LEN; i++){
55
56    // Put the array elements in order
57    twoMicSamples[0] = *(rx_msg->data + i*4 + 2);
58    twoMicSamples[1] = *(rx_msg->data + i*4 + 3);
59
60    tx_msg->data[i*4] = 0;
61    tx_msg->data[i*4 + 1] = 0;
62    tx_msg->data[i*4 + 2] = sum(twoMicSamples, delay);
63    tx_msg->data[i*4 + 3] = 0;
                                                                                            16
                                                A PPENDIX D
                                        C CODE FOR SUMMING DELAYS
     Listing 1.   sum.h
1    #ifndef _SUM_H_BFORM_
2    #define _SUM_H_BFORM_
3    extern Int16 sum(Int16* newSamples, int delay);
4    #endif
     Listing 2.   sum.c
 1
 2   #include <std.h>
 3
 4   #include "definitions.h"
 5   #include "calcDelay.h"
 6
 7   /* newSamples is an array with each of the four samples.
 8    * delayIncrement is the amount to delay each microphone by, in samples.
 9    * This function returns a "beamed" sample. */
10   Int16 sum(Int16* newSamples, int delayIncrement){
11           static int currInput = 0; // Buffer index of current input
12           int delays[NUMMICS]; // Amount to delay each microphone by
13           int mic = 0; // Used as we iterate through the mics
14           int rolloverIndex;
15           Int16 output = 0;
16           static Int16 sampleBuffer[NUMMICS][MAXSAMPLEDIFF];
17
18            // Calculate samples to delay for each mic
19      // TODO: Only do this once
20            calcDelay(delayIncrement, delays);
21
22      // We used to count backwards - was there a good reason?
23            currInput++; // Move one space forward in the buffer
24
25      // Don’t run off the end of the array
26      if(currInput >= MAXSAMPLEDIFF){
27        currInput = 0;
28      }
29
30              // Store new samples into sampleBuffer
31              for(mic=0; mic < NUMMICS; mic++){
32                      // Divide by the number of microphones so it doesn’t overflow
33          // when we add them
34                      sampleBuffer[mic][currInput] = newSamples[mic]/NUMMICS;
35              }
36
37                  // For each mic add the delayed input to the current output
38                  for(mic=0; mic < NUMMICS; mic++){
39                          if(currInput - delays[mic] >= 0){// Index properly?
40                                  output += sampleBuffer[mic][currInput - delays[mic]];
41                          }
42                          else{
43                // The delay index is below 0, so add the length of the array
44                // to keep it in bounds
                                                                                          17
     Listing 3.   calcDelay.h
1    #ifndef _CALC_DELAY_H_
2    #define _CALC_DELAY_H_
3    extern void calcDelay(int delayInSamples, int* delays);
4    #endif
     Listing 4.   calcDelay.c
 1   /* calcDelay
 2    * Accepts delays in samples as an integer
 3    * and returns a pointer to an array of delays
 4    * for each microphone.
 5    *
 6    * Date: 9 March 2010
 7    */
 8
 9   #include "definitions.h"
10
11   void calcDelay(int delayInSamples, int* delays){
12           int mic = 0;
13           if(delayInSamples > 0){
14                   for(mic=0; mic < NUMMICS; mic++){
15                           delays[mic] = delayInSamples*mic;
16                   }
17           }
18           else{
19                   for(mic=0; mic < NUMMICS; mic++){
20                           delays[mic] = delayInSamples*(mic-(NUMMICS-1));
21                   }
22           }
23   }
                                                A PPENDIX F
                                             JAVA C ODE - M AIN
     Listing 5.   MainWindow.java
1    /*   MainWindow.java - Java class which constructs the main GUI window
2     *   for the project, and sets up communication with the DSK. It also
3     *   contains the main() method.
4     *
5     *   Author: Steven Bell and Nathan West
6     *   Date: 9 March 2010
7     *   $LastChangedBy$
8     *   $LastChangedDate$
                                                                                       18
 9       */
10
11   package beamformer;
12
13   import java.awt.*; // GUI Libraries
14
15   import javax.swing.*;
16
17   public class MainWindow
18   {
19     JFrame window;
20     DisplayPanel display;
21     ControlPanel controls;
22     static Beamformer beamer;
23
24       MainWindow()
25       {
26         beamer = new Beamformer();
27
28            window = new JFrame("Acoustic Beamforming GUI");
29            window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
30            window.getContentPane().setLayout(new BoxLayout(window.getContentPane(), BoxLayout.LINE
31
32            display = new DisplayPanel();
33            display.setPreferredSize(new Dimension(500,500));
34            display.setAlignmentY(Component.TOP_ALIGNMENT);
35            window.add(display);
36
37            controls = new ControlPanel();
38            controls.setAlignmentY(Component.TOP_ALIGNMENT);
39            window.add(controls);
40
41            window.pack();
42            window.setVisible(true);
43
44            beamer.start();
45       }
46
47       public static void main(String[] args)
48       {
49         MainWindow w = new MainWindow();
50         while(true){
51           beamer.update();
52           w.display.updatePower(beamer.getPower());
53         }
54       }
55   }
                                                     A PPENDIX G
                                    JAVA C ODE FOR B EAMFORMER C OMMUNICATION
     Listing 6.   Beamformer.java
1    /* Beamformer.java - Java class which interacts with the DSK board
2     * using RTDX.
3     *
                                                                                  19
 58       {
 59           System.out.println(e.toString());
 60       }
 61   }
 62
 63   public void start()
 64   {
 65     // Start running the program on the DSK
 66     try{
 67       debugSession.target.restart();
 68       System.out.println("Target restarted.");
 69       debugSession.target.runAsynch();
 70       System.out.println("Program running....");
 71       Thread.currentThread().sleep(1000); // Wait a second for the program to run
 72     }
 73     catch (Exception e)
 74     {
 75       System.out.println(e.toString());
 76     }
 77   }
 78
 79   public void stop()
 80   {
 81     // Stop running the program on the DSK
 82     try{
 83       debugSession.target.halt();
 84       System.out.println("Program halted.");
 85     }
 86     catch (Exception e)
 87     {
 88       System.out.println(e.toString());
 89     }
 90
 91   }
 92
 93   public void setMode()
 94   {
 95
 96   }
 97
 98   public void setNumBeams()
 99   {
100
101   }
102
103   public void update(){
104     try{
105       // Read some bytes
106       byte[] power = new byte[1];
107       byte[] delay = new byte[1];
108
109           inStream.read(delay, 0, 1, 0); // Read one byte, wait indefinitely for it
110           inStream.read(power, 0, 1, 0); // Read one byte, wait indefinitely for it
111
                                                                                            21
112                int intPower = power[0] & 0xFF; // Convert to int value from unsigned byte
113
114                mPowerValues[delay[0] + 33] = intPower; // Save them
115
116          System.out.println("D:" + (int)delay[0] + "                P: " + intPower);
117        }
118        catch (Exception e)
119        {
120          System.out.println(e.toString());
121        }
122      } // END update()
123
124      public int[] getPower()
125      {
126        return mPowerValues;
127      }
128
129      /*
130      public int[] getDelays()
131      {
132         return int
133      }*/
134
135   } // END class
                                                 A PPENDIX H
                                        JAVA C ODE FOR B EAM D ISPLAY
      Listing 7.   DisplayPanel.java
  1   /* DisplayPanel.java - Java class which creates a canvas to draw the
  2    * beamformer output on and handles the drawing.
  3    *
  4    * Author: Steven Bell and Nathan West
  5    * Date: 9 March 2010
  6    * $LastChangedBy$
  7    * $LastChangedDate$
  8    */
  9
 10   package beamformer;
 11
 12   import java.awt.*; // GUI Libraries
 13   import javax.swing.*;
 14   import java.lang.Math;
 15
 16   public class DisplayPanel extends JPanel
 17   {
 18
 19      int mDelay[];// = {-3, -2, -1, 0, 1, 2, 3};
 20      double mPower[];// = {0, .25, .5, 1, .5, .25, 0};
 21      int mNumPoints = 67;
 22
 23      double mSpacing = 33;
 24
 25      DisplayPanel()
 26      {
                                                                                  22
 81         g2d.setPaint(Color.BLUE);
 82         g2d.drawPolygon(px, py, mNumPoints);
 83
 84         // Draw the outline of the display, so we have some context
 85         g2d.setPaint(Color.BLACK);
 86         float[] dash = {5, 4};
 87         g2d.setStroke(new BasicStroke((float).5, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
 88         g2d.drawLine(10, centerY, this.getWidth() - 10, centerY);
 89         g2d.drawArc(10, (this.getHeight() - maxRadius) / 2, 2*maxRadius, 2*maxRadius, 0, 180);
 90     }
 91
 92     // Takes a delay and converts it to the equivalent in radians
 93     private double delayToAngle(int delay)
 94     {
 95       return(Math.PI/2 - Math.acos(delay/mSpacing));
 96     }
 97
 98     // Takes an angle in radians (-pi/2 to +pi/2) and converts it to a delay
 99     private int angleToDelay(double angle)
100     {
101       return(int)(mSpacing * Math.cos(angle + Math.PI/2));
102     }
103 }