Tuesday, October 23, 2012

DIY sonar using PC speaker and microphone

Together with my friend and colleague John Berezney, I decided to make a sonar system using a microphone and speaker. The idea is to make an active sonar system using PC speakers as output and a microphone as an input. I got some inspiration for this project from this website: http://eddiem.com/projects/chirp/chirp.htm

The first step is to make a chirp, which is just a type of pulse that changes frequency (see Figure). The audio speakers I have can only make pulses up to 20kHz (I think?), so I make chirps that vary between around 4kHz and 16kHz. I used Labview software to program the chirp and make it come out of my speakers.
The equation for this chirp, C(t) is a function of the pulse length T as well as the minimum and maximum frequencies in the chirp f_min and f_max. Typically I would choose a pulse length of T=2 milliseconds and f_min=4kHz and f_max=12kHz. The first part of the equation with the exponential is just an envelope function that prevents the frequency content from leaking into adjacent frequencies. The equation is:

Once we have a chirp coming out of the speakers, the next trick is to process the recorded audio that is coming in through the microphone. There are many ways to do this. I chose to use a matched filter. The idea is that whenever the microphone picks up a chirp that matches the one I sent, it makes a spike. This trick (matched filters) is used in radar all the time. The math for turning the microphone recording R(t) into a useful sonar signal S(t) using a matched filter is:

Note that we have to take the complex conjugate of the Fourier tranform of the chirp signal, multiply that by the Fourier transform of the Recording in frequency space, and then inverse Fourier transform to get a signal as a function of time. The signal S(t) has spikes wherever there is a reflection. Here you see S(t) which has a strong signal where the microphone picks up the direct wave, and a weaker signal for all the reflections.

The final step is to keep making chirps (my program could make around 4 per second) and then plot the intensity vs distance vs time (yes a 3D graph on a 2D plot!). To do this, I made an intensity graph, where height represents distance away in the reflection, and the time scrolls horizontally. I was holding a sound-reflecting clipboard, and walked away around 3 meters, and then returned to the computer. You can see that as a high intensity reflection going vertical on the graph (far away) and then closer.

John and I have some fun ideas for improving our sonar, I will keep the world updated as we make more improvements! I would be happy to hear if you guys have some fun ideas for improvements! I did all the signal processing and speaker output using Labview software. I am happy to give you the source code if you are interested.

33 comments:

  1. Hi, This is very interesting. I have little experience with LabView (mostly Matlab till now and I am hoping to learn). Would it be possible to share the code? You can send at hsuri@ufl.edu. Thanks.

    ReplyDelete
    Replies
    1. I'm very happy to see that someone is interested in this random project! I added a link to the files here: https://docs.google.com/folder/d/0B0twtL0ZJcByemZUczRqRWoxV1k/edit Please let me know if you have any problems getting it to work, and what you end up doing with it since I am curious. Cheers!

      Delete
    2. Hi Bob, Thanks for the link but it does not appear to work. It is giving me access denied error, not sure if it is an artifact of my browser or some setting error. Please let me know. Thanks again.

      Delete
    3. OK I changed the settings, the link should work now! Let me know if you have any other issues with the code.

      Delete
    4. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. best pc microphone

      Delete
  2. apparently the file seems to be an image and i can't download or add to my drive

    ReplyDelete
  3. The link in my previous comment goes to Labview .vi files. Does that answer your question?

    ReplyDelete
  4. I don't seem to be getting anything that matches your Intensity graph as I walk away and close to the mic.
    Can I send you a screen shot of the intensity graph , maybe you can help me?
    Thanks

    ReplyDelete
    Replies
    1. please, do share your images, I will post them and try to help you debug

      Delete
    2. I have sent you a screen shot of the Intensity Graph in your 'HangOut' messages part in Gmail.
      Please could you let me know what I may be doing wrong.
      Thanks

      Delete
    3. I never heard from you... if you still want to debug, please post your screenshot somewhere that I can access it.

      Delete
  5. Very interesting, How do you keep the microphone from picking up the chirp directly from the speaker and just focus on the reflected sound?

    ReplyDelete
    Replies
    1. Thanks for you interest. To answer your question: I don't. The first peak is the direct wave, and it is much stronger than the reflected waves.

      Delete
    2. I Have it working properly, and I compiled the .exe but I don't have near the resolution you have on your intensity chart screenshot, having a hard time telling the waves apart. Any Ideas?

      Delete
    3. Screenshot

      www.doverparanormal.96.lt/Untitled.jpg

      Delete
    4. Maybe it's just a question of adjusting the levels that are displayed. Maybe try using a logarithmic chart: "mapping: logarithmic". or when you right click on the legend, turn off "autoscale Z" and use a small range that will show the small reflections more clearly. Also, cranking up the volume on the speaker will give you the best chance of picking up a signal.

      One odd thing is that you have your samples/second set at 15,000, whereas your max frequency is 14,000. By the Nyquist criteria, you should either up your samples/Second above 28,000, or lower your max freq to below 7500.

      Also, I never did this, but you could find the max peak (direct reflection) and line up your charts to that so that they have the same absolute distance. At the moment, the direct wave is not a perfectly horizontal line because of timing inconsistencies between the speaker out and microphone in. A few milliseconds makes all the difference.

      Let me know how it goes!

      Delete
    5. The helped greatly, thanks. I am not sure if it is my microphone or if something else is at play, The only way I can receive a clear echo is if I use a large reflective panel. Maybe this is normal....I mean it is sound we are using. I did have a problem with what I think was reverb, I moved to a larger room and it helped.

      Delete
    6. Great! I think it's normal that the signal are weak, for my data I was holding a reflective clipboard that was pointing at the microphone and the chirps were almost painfully loud!

      You could try experimenting with the chirp duration, and also your sampling rate. My intuition is that higher sampling rate is always better, and that chirp duration longer is better, but that when chirps get too long it will be difficult to distinguish two independent scatters. So if you use chirp of 10ms duration, the best resolution you get is 0.01ms*330m/s = 3.3m. Maybe that's OK! But I don't know for sure. Maybe longer chirps are OK... Maybe you could figure it out for us :)

      Can you share your data? I'd be curious to see what you managed to get!

      Delete
    7. any luck with playing with the chirp length?

      Delete
  6. Has anyone thought about playing with an Aliens M314 scanner? I think the idea of a 2 mic, 2D sonar that also can detect and show doppler shift would also be a fun project to play with.

    ReplyDelete
  7. Hi, can you tell me what was the intensity of the sound used in this project? Also have you figured out sound intensity treshold for decent results?

    ReplyDelete
    Replies
    1. I don't know. With the low-quality microphone I was using, I had to crank up the speakers and used a clipboard as a reflector to be able to measure anything.

      Delete
  8. Would be interesting to convert this to a frequency-swept continuous-wave system. The reflected wave interferes with the sending wave (maybe requiring some hardware modifications?) and the phase difference tells you how far away an object is (simplified) - as if your sweep rate of the frequency & detected frequency tells you the time delay w/r/to the sweep rate of your output frequency. In theory I believe this gives higher SNR and Dynamic Range.

    I wondered about this difference a lot on optical coherence tomography, which is an optical analogue to Sonar/Ultrasound.
    — Demis

    ReplyDelete
    Replies
    1. Let's explore that idea. In optics, the fundamental carrier wave (photons) is much too high-frequency to detect directly, and so interference techniques have to be used. In acoustics, the frequencies are within the range of ADC frequency (e.g. a microphone). So to make the system analogous, let's not get too crazy and introduce a 1 MHz resonator that can be modulated at acoustic frequencies or anything. Let's choose a "carrier wave" of like 10kHz that is also audible. Then, "frequency scanning" that carrier wave between 1-2000 Hz or something would enable this system to work. Or just simply adding some 1-2000Hz noise onto the 10kHz carrier wave. Either way, the pulses would sound like a high-frequency tone that lasts for a long time. One advantage of this right away is that you are probing acoustic reflectivity in a narrower range, which might improve fidelity of the signal. It would probably be important to have the right amount of bleed in of direct-wave into the microphone. So maybe being more careful about the acoustic isolation of speaker and direct wave into microphone could give a hardware-analog to the fiber-coupler tuning of direct and reflected with in OCT. Finally, performing the same type of Fourier analysis to get depth resolved intensity. Fundamentally, you'd be getting more signal if you use a longer pulse so I think this should work. You would lose a bit on "moving" targets since the fundamental assumption is that nothing's moving. But to start with, it would probably get much better results than what I was able to do. I imagine that it could be difficult to prevent the "direct wave" from swamping the reflected signal.

      Actually, what you are describing could be done simply by modifying the pulse to include a higher frequency carrier wave. Either way, I would be very curious to see the results if you do try this!

      Delete
    2. Another thing that could help would be electrically isolating the speaker and microphone. In addition to acoustic bleed-in, I suspect that there was electrical bleed-in from the speakers to microphone through my bad-quality sound card.

      Delete
  9. nice project bob, keep update

    ReplyDelete
  10. This is really interesting, I recently started working on a similar project. I am having troubles analyzing the signals without labview ... do you know softwares that could work the same ? I have not checked but I guess labview isn't opensource
    Paul

    ReplyDelete
    Replies
    1. Thanks! Pretty much any software will do, I just chose Labview because it's pretty quick to make something work. You just need to find a way to get audio going out and audio streaming in. You could probably even make a smartphone app that you hook up to a speaker if you were so inclined.

      Delete
  11. Amazing. Do you think that this can work underwater? I'm thinking about creating a ROV with a sonar to be able to find underwater objects or fishes.

    ReplyDelete
    Replies
    1. Underwater sonar is definitely a thing! Usually they use higher frequencies of sound called "ultrasound."

      Delete
    2. Hi Bob. I think it is very cool that you have been replying to posts so many years after writing the article. Great article by the way! I've been wondering if it is possible to create a microphone array using standard PC parts, but I think maybe the timing will not be precise enough?

      Delete
  12. Hey im or her thinking of buying this microphone... looks pretty good and i produced good experience with hyperX. Can anyone answer me the actual question about keyboard sounds. I would place behind our keyboard... will the noise always be to loud and made my good friend in discord complain over it? hyperx quadcast amazon

    ReplyDelete
  13. M-Audio BX8D3 Speakers in UAE, Professional Multimedia Speakers in UAE, Speakers in UAE
    https://gccgamers.com/m-audio-bx8d3.html
    M-Audio BX8D3 Speakers in UAE, Safe Shopping Multiple Payment Options Express Delivery GCC Gamers Moneyback Guarantee.
    1633433905975-8

    ReplyDelete