# Microphone Spectrum

I need help interpreting the data points captured by the Microphone Spectrum function. The illustration below shows the results for a 500 Hz tone (left) and a 1000 Hz tone (right). The 500 Hz tone appears to peak at about Item 12 in the list and the 1000 Hz tone appears to peak at about Item 22 in the list. How can I interpret this result? In other words, how can the items in the list be mapped to the range of frequencies captured by the microphone?

Hi Glen,

first of all, I have no idea . About ten years ago I went through the motions of analyzing the frequency spectrum to determine the dominant frequency, and I've forgotten all about it except that it was a totally horrible procedure. So, truth be told, we're basically letting you play with the frequency spectrum because we're getting it for free from web-audio anyway. You can use it for some fun visualizations and even interactive games. But other than that I'm - frankly - clueless myself.

Personally I'm using a pretty simply auto-correlation algorithm on the actual samples to find out the likely frequency that's picked up by the microphone. I got that formula from combining stuff I read on Wikipedia and about half a dozen other sites and ... drum roll ... Stack Overflow. The beauty of the auto-correlation check on zero-passes is that I can do it all myself in code and tweak it so it works best for the given buffer size etc. That way to determine a frequency from samples is actually ... kinda nice! And maybe even a nice activity for a media-comp project.

Thanks ... this gives us a starting place as we explore possible ways to incorporate this into our projects. The frequency that you're obtaining from the auto-correlation algorithm seems to be spot on. A 200 Hz tone is shown as exactly as 200 Hz in your function ... even when there are higher overtones in the sound.

We were considering use of the spectrum output to obtain the relative weightings of the high, low, and mid frequencies in the signal for use in a light show ... but possibly we could adapt the frequency output for this instead (i.e., the color of the light could change as the frequency varies over time).

Thanks!

So if you know that a tone is 200 Hz, and the peak of the spectrum array is at item 6, then each step in the array is a change by 200/6 Hz. In your actual example in the original post, it was 500 Hz and peak at 12, so it'd be a step of 500/12 Hz per item.

Yes, that seems to be the case. Here are snapshots for 1,000, 5,000, 10,000, and 20,000 Hz. The range covered appears to be approximately 20 kHz.

20,000 Hz / 512 steps = 39 (approximately), which is in the ballpark for 500 / 12 = 42 (rounded).

Hi,

• Your maximum recorded frequency depends on your "sample rate". Just 1/2 of the sample rate. Then, working at 44100Hz, you have 22050Hz is your maximum working sound frequency.
• Aside this, you can choose your "resolution". See "Settings menu -> Microphone resolution..." Low, normal, high and max are really 256, 512, 1024 and 2048 samples/steps. Snap! uses this value for the number of frequencies in the "spectrum" reporter (and also, of course, into its calculation (using fast fourier transformations - fft))
Then, your frequency steps are (sample rate/ 2 / resolution). Yes "rounded 43" for the default state... but remember you can change your resolution!

Joan

Thanks for this background context. We used the Microphone [Spectrum] input to control the intensity of three LEDs corresponding to the high, low, and mid-range. Here's an initial YouTube video that we created as a proof-of-concept:

1. Is it possible to set the Microphone Resolution via a script?

2. Is it possible to determine the current Microphone Resolution via a script?

Thanks.

There's a MICROPHONE reporter block in Sensing. I'm not seeing a way to set it programmatically; eventually I'll get around to adding it to the getter/setter library.

Thanks!

Hi @glenbull,
As brian's said, you have the resolution reporter

And, if you need that setter before it's in the library, I share a workaround

Joan

I've deliberately set the microphone resolution to 512, which is slightly too low for most microphones, so things like pitch-detection, volume etc. can be performed "live" on most computers. You can play with the setting in the menu, but I advise against setting it programmatically. Contrary to intuition increasing the resolution does - in most cases - not improve pitch detection.

... but maybe you want to print a plot of the spectrum.

sure, 512 bins should be more than enough for a graphics output. What I'm saying is, please don't offer any programmatic way to mess with settings because people will believe that more is better, when, in fact, it's not.

Why isn't that true about the UI way? Either way, you're offering people that choice, and if you really think you shouldn't offer it... etc.

Okay, I'm taking it out.

Currently I am using the low Microphone Resolution Setting, and graphing the first 128 bins. If my understanding is correct, this should display from 0 to 12,000 Hz. Is that correct? Thanks.

This graph of a note played on a ukulele clearly shows the overtones.

It's interesting that they're spread out. I would expect that in a piano, with two or three strings per note deliberately tuned slightly differently, but how does it happen on a ukulele? Are we seeing your finger sliding a little, or is there a low-Q amplifier in the path, or would the graph be better with 1024 buckets, or what?

The instrument was an acoustic ukulele with nylon strings. A string plucked in this manner vibrates in multiple modes, generating a series of overtones that are multiples of the fundamental frequency.