I am trying to actuate a simple DC motor based on a wav file. At the moment I am using the "cat vs dogs sounds" from Kaggle.

One of the wav files in this DB looks like this:

enter image description here

These audio files are just a few seconds. The sample rate is 16KHz.

My first idea was to just actuate the motor based on the amplitude, but at 16KHz this doesn't make any sense. Also, the signal goes constantly from + to - (this is sort of ok because the motor can move in both directions), but from a human POV, what the motor does doesnt make any sense, because the movements are too rapid. I then tried to "decimate" the signal into a few points per second, but the signals seem to be badly destroyed.

DC motors are actuated using an Adafruit board, using Arduino, and then I have a Python script that interfaces with the Arduino through serial. I have this working fine, and I can make for example a loop from python that increases the speed every few seconds. My idea was to go forward or backwards based on the amplitude of the signal (if it is negative or positive), I am no expert on audio, so this might make no sense. I won't react to other sounds, only to the input wav files.

I don't really know what is the best way of transforming an audio signal of 16KHz into something that can be use to actuate a motor. At the moment I am using a DC motor, but the idea in the near future is to use leds instead, and change their intensity based on the audio signals


closed as unclear what you're asking by Stanley Pawlukiewicz, MBaz, lennon310, Peter K. Jul 23 at 23:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ What is your objective? Is it simplicity and entertainment value? $\endgroup$ – Olli Niemitalo Jul 13 at 10:08
  • 1
    $\begingroup$ Just entertainment. The idea would be to, in the future, change the brightness of some LEDs based an audio signal. $\endgroup$ – Dr Sokoban Jul 13 at 11:56
  • 1
    $\begingroup$ Would it be safe to say that your objective is to obtain a sequence which roughly follows the envelope of your sound signal? At a much slower sampling rate? $\endgroup$ – Cedron Dawg Jul 13 at 13:12
  • $\begingroup$ @CedronDawg yes exactly. but when I try to do something like a moving average, or decimate, because the signal goes from + to - as the amplitude goes, the results are very bad and it doesn't look like the signal $\endgroup$ – Dr Sokoban Jul 13 at 13:57

Well, if you are okay with a signal that follows the intensity instead of the envelope, which will be roughly the same, there is a simple trick to it.

Suppose you want one value every tenth of a second. For each value, find the sample location at that time. Multiply your signal by a suitable bell shaped curve centered at that point. A VonHann window would work nicely. Make sure the window interval is longer than several cycles of your sound signal. Now, add the squares of the multiplied values over your windowed span. Divide this sum by the number of samples in your window and take the square root. This is the RMS (Root Mean Square, literally the square root of the mean of your squares) of your windowed signal. Use this, or rescale it some, for your driving signal.

You will always get non-negative values. Zero when the signal is zero, proportional to the amplitude for steady tones independent of frequency.

Here are the formulas:

$$ w[h] = \cos^2 \left( \frac{h}{2H} \pi \right) $$

$$ y[n] = \sqrt{ \frac{ \sum_{h=-H}^{H} ( w[h] x[n+h] )^2 }{ 2H+1 } } $$

The value of $H$ will determine how locally sensitive your measure is. For H = 0, you get the absolute value of the signal. Which suggests an alternative. You could also simply use a weighted average of absolute values. It would be comparable in results and more efficient to calculate.

The important thing is that $H$ be large enough to span several cycles of the underlying fundamental tone.

To make it even more efficient, you can skip a sample or two or three, as if your signal had a lower sampling rate. But not too much, or results will suffer.

  • $\begingroup$ I don't understand your answer. What's w, y, h, n, x? $\endgroup$ – Dr Sokoban Jul 15 at 15:53
  • $\begingroup$ @DrSokoban x is your audio input value at sample n, y is your output "intensity metric" at sample n, w is a set weights also known as a window, n is the sample index, h is an iteration variable. The equations are the math version of the paragraph above. The formula requires samples "into the future" ($h>0$), so there is a latency of H if you are doing this real time. You can calulate this value at whatever rate you like. You might also want to just simply take the average of the absolute values over an interval, weighted or not, instead of RMS. For this it might work just as well. $\endgroup$ – Cedron Dawg Jul 15 at 16:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.