line in sound - get frequency (spectrum) analysis, pitch

Hi guys, noob here.

I want to get a spectrum analysis on my arduino done, or just simply, the pitch of the sounds played.

I have seen a solution, using a microphone:

... but I want to make it without a microphone.

(In short: arduino, using a microphone prepared for use with arduino, reads the input on an analog input, gets with fast fourier transformation the frequency of the input signal, and shows on an OLED screen.)

Simply put, I wired the sound output of my laptop to the analog input of the arduino, and tried the code from the youtube video above ( https://www.dropbox.com/sh/m6c40pu99fqxb5b/AABAQTv67pbYOl6gNozWwDNLa/spectrum?dl=0&preview=spectrum.ino ), but using the FFT (fast fourier transformation), I get wrong results.

After tweaking I got it working, but I don't understand the math at all, and my numbers don't add up. I send out 4500 Hz to the arduino, but get a peak at the 95% of the spectrum analysis, if I raise the pitch on the source (using just 1 sine generator sound), the spectrum "reverses", on 4800->5200->... the peak on the chart moves LEFT. Then at 9000 Hz it turns again, moves to right again, and so on.

I have a 2. peak too, with half of the height of the main peak, at the half frequency of it. Like when 4500 Hz is the peak, I see a half that high peak at 2250 Hz (could be that I misread it).

(I am using a FiiO K1 USB DAC, the volume boost on maximum gets the peak around 60% of the chart. The height of the peak represent the volume rightly, I tested it. The power seems enough.)

Right now I have no other IC or anything, if I can, I don't want to use it first. I have no knowledge of circuit design.

(The code above filters out the 50-60 Hz peak, please check the code for more details. The OLED screen is not the issue here, just getting the fourier transformation and the math right.)

I have no knowledge of circuit design.

Obviously not. You are damaging the analog input of the Arduino with negative voltages.

I send out 4500 Hz to the arduino, but get a peak at the 95% of the spectrum analysis, if I raise the pitch on the source (using just 1 sine generator sound), the spectrum "reverses", on 4800->5200->... the peak on the chart moves LEFT. Then at 9000 Hz it turns again, moves to right again, and so on.

This is the classic behavior for aliasing, which results from frequencies higher than (1/2) the sampling frequency getting into the input.

Putting negative voltage signals into the Arduino will do that, as well as damage the input.

We have been here before on other postings. Pitch is not something to be measured. It is a human perception of sound. Look at the definition.

Paul

jremington:
This is the classic behavior for aliasing, which results from frequencies higher than (1/2) the sampling frequency getting into the input.

Putting negative voltage signals into the Arduino will do that, as well as damage the input.

Ok, than I assume the microphone board got rid of the negative voltages (I don't know if it was shifting the voltage upper, or just cutting it), how can I do that? After it, what more do I need?

About the damage: how can I see or test if I already damaged the board?

Which microphone board? How do you have it wired?

For informed help, please read and follow the directions in the "How to use this forum" post.

jremington:
Which microphone board? How do you have it wired?

For informed help, please read and follow the directions in the "How to use this forum" post.

Originally, in the opening post, I wrote that I initially tried copying the project seen in the youtube video. Every details (arduino .ino code, used parts) are there, the microphone is here: https://www.amazon.com/gp/product/B00K9M6S1O .

And what I want, is written after the video link, I want to replace the microphone with the audio output of a laptop (headphone output, but slightly amped).

Please say what more information I can give.

Most forum members aren't interested in watching a video, just so they can figure out what some poster has tried to do.

If you want help on this forum, some effort on your part, to present your particular problems sensibly, is required.

Hi,

I guess you are taking the headphone output of your USB DAC and feeding that into an Arduino Analog input.

The somewhat cryptic posts mentioning 'damage' and 'negative voltages' refer to the fact that the audio signal to drive headphones will be AC, it will swing +ve and -ve.

The process of reading an analog signal for, in this case, doing an FFT using the Arduino's Analog to Digital converter needs a +ve only signal between 0V and 5V. The convention is that 2.5V is taken as the 'zero signal' level, above 2.5V is +ve signal and below 2.5V is -ve signal.

You are sort of correct when you said:

Ok, than I assume the microphone board got rid of the negative voltages

You'll have to make up a small circuit to bias the analog input to 2.5V and couple in the audio signal with a capacitor. Like:
Diagram:
AnalogInput.png
Yours,
TonyWilk

AnalogInput.png

TonyWilk:
Hi,

I guess you are taking the headphone output of your USB DAC and feeding that into an Arduino Analog input.

The somewhat cryptic posts mentioning 'damage' and 'negative voltages' refer to the fact that the audio signal to drive headphones will be AC, it will swing +ve and -ve.

The process of reading an analog signal for, in this case, doing an FFT using the Arduino's Analog to Digital converter needs a +ve only signal between 0V and 5V. The convention is that 2.5V is taken as the 'zero signal' level, above 2.5V is +ve signal and below 2.5V is -ve signal.

You are sort of correct when you said:

You'll have to make up a small circuit to bias the analog input to 2.5V and couple in the audio signal with a capacitor. Like:
Diagram:
AnalogInput.png
Yours,
TonyWilk

First of all, huge thanks.

After assembling this, I will get a readable range of values on the arduino? Could you please check the code ( Dropbox - spectrum - Simplify your life ), will it work with this modified value range, or will I need to change the values back on the arduino end?

cherockee:
After assembling this, I will get a readable range of values on the arduino?

It's a bit of a guess, but there should be enough signal to get some sort of output from the FFT.

It depends a lot on the output of the FiiO K1 USB DAC you have, according to the K1's datasheet you should get about 1Volt signal with that 15ohm resistor. If the maximum signal you get is far too small, you could up that to 100ohm, since the datasheet implies it could then output 3.25 Vp-p

Yours,
TonyWilk

Be aware you already have a digital component to the audio signal. You will probably see it in your analysis.

Paul

Paul_KD7HB:
Be aware you already have a digital component to the audio signal. You will probably see it in your analysis.

Paul

The maker of the arduino code already filtered out in the visualization the first bar of the chart, using just the 63 sample from the 64 sample collected, that filtered it out as far as I seen.

Could you point in the direction on how to make it not having a digital component? My knowledge is not deep, as you have seen, I just guess that I need to get both the source of the audio signal and the arduino off of DC power, but I am not sure that I have to, for both. And I don't know if there is any solution to get a proper, uncorrupted reading of the signal using DC powered components. (If I understand right, using DC power, I will always get a 50 Hz peak, but then I can't know for sure if that is because of the audio signal or just the DC.)

I will always get a 50 Hz peak

That would come from the household AC (wiring, overhead lights, etc.). If in doubt, power with batteries.

jremington:
That would come from the household AC (wiring, overhead lights, etc.). If in doubt, power with batteries.

Do I need to use battery power on both end, on the source of the signal, and the arduino, or just on the arduino? Currently the arduino runs on usb from the laptop where the sound comes from.

House AC is most likely to have an effect on the (input) audio signal pathways.

The problem is the same as you see when you scan a digitally printed digital picture.

Paul