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.

20 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
  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. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you must watch this video
    right away...

    (VIDEO) Get your ex back with TEXT messages?

    ReplyDelete