What is AVAudioPlayer's averagePowerForChannel averaging? - iphone

The docs for AVAudioPlayer say averagePowerForChannel: "Returns the average power for a given channel, in decibels, for the sound being played." But it doesn't say what "average" means, or how/if that average is weighted. I'm particularly interested in what the range of samples is, specifically the time interval and whether it goes forward into the future at all.
AVAudioRecorder: peak and average power says that the formula used to calculate power is RMS (root mean square) over a number of samples, but also doesn't say how many samples are used.
Optional bonus question: if I want to calculate what the average power will be in, say, 100 msec -- enough time for an animation to begin now and reach an appropriate level soon -- what frameworks should I be looking into? Confusion with meters in AVAudioRecorder says I can get ahold of the raw sample data with AudioQueue or RemoteIO, and then I can use something like this: Android version of AVAudioPlayer's averagePowerForChannel -- but I haven't found sample code (pardon the pun).

Related

How to calculate amplitude from Uint8List Stream?

I am using flutter_sound package to record audio from mic. It provides data in stream of Uint8List. So how can I calculate amplitude from it. I have found many answers in other language but I was having hard time interpreting it into dart.
for reference,
Reading in a WAV file and calculating RMS
Detect silence when recording
how can i translate byte[] buffer to amplitude level
if anyone can interpret this into dart so that I can calculate amplitude
I was not able to calculate amplitude from Uint8List so what I did was write my own native code to directly get amplitude from native you can check out my package here. (I am busy somewhere else so I'm not right now maintaining it but will surely do in future.)
And I don't know but flutter_sound provides maybe false values or I was calculating wrong but I was always getting nearly the same values even when the volume is high or low.
Now, if you want to calculate amplitude then it should be based on values you're getting,
If you are getting values between -32768 and 32767 then from my understanding what you do is define a timeframe and for that timeframe calculate the RMS of it.
The RMS is your amplitude.
Now, as #richard said in the comment to normalize with 32768 if you have Int16List. But I guess that is doing too much work. We get a stream of Int16List so data may be coming every 200ms or whatever it is set at the plugin level. Calculating RMS for each Int16List is a very intensive task so I don't think it's good to do it on the flutter side. Get data from native directly and just pass it flutter using platform channels.
Now, if you want to try then calculate the RMS of an Int16List then for a defined timeframe take the mean of those values.
I may well be wrong but hope this helps or guides you in the right direction. If anyone finds a better way please post it here.

How to get the reading of deciBels from iOS AVAudioRecorder in a correct scale?

I'm trying to obtain a noise level in my iOS app, using AVAudioRecorder.
The code I'm using is:
[self.recorder updateMeters];
float decibels = [self.recorder averagePowerForChannel:0];
// 160+db here, to scale it from 0 to 160, not -160 to 0.
decibels = 160+decibels;
NSLog(#"Decibels: %.3f", decibels);
The readings I get, when the phone sits on my desk are at about 90-100dB.
I checked this this link and the table I saw there shows that:
Vacuum Cleaner - 80dB
Large Orchestra - 98dB
Walkman at Maximum Level - 100dB
Front Rows of Rock Concert - 110dB
Now, however my office might seem to be a loud one, it's not near the walkman at maximum level.
Is there something I should do here to get the correct readings? As it seems my iPhone's mic is very sensitive. It's an iPhone4S, if it makes a difference.
Forget my previous answer. I figured out a better solution (correct me if I am wrong). I think what both of us want to achieve is the decibel SPL but the averagePowerChannel method gives us the mic's output voltage. The decibel SPL is a logarithmic unit that indicates ratio. We need to convert that output in decibel SPL which is not so easy because for that you need reference values. In other words you need a DB SPL values and the according voltage values to them. You can also try to estimate them by comparing your results with an app like decibel Ultra. To come straight to the point: The formula you need is as follows:
SPL = 20 * log10(referenceLevel * powf(10, (averagePowerForChannel/20)) * range) + offset;
you can set the referenceLevel to 5. That gives me good results on my iPhone. The averagePowerForChannel is the value you gain from the method averagePowerForChannel: method and range indicates the upper limit of the range. I set that to 160. Finally offset is an offset you can add to get into the area you want. I added 50 here.
Still, if anybody got a better solution to this. It would be great!

Sine LUT VHDL wont simulate below 800 hz

I made a sine LUT for VHDL, using 256 elements.
Im using MIDI input, so values range 8.17Hz (note #0) to 12543.85z (note #127).
I have another LUT that calculates how many value must be sent to my 48 kHz codec in order to play the sound (the 8.17Hz frequency will need 48000/8.17 = 5870 values).
I have another LUT that contains an index factor, which is 256/num_Values, which is used to call values from the sin table (ex: 100*256/5870 = 4 (with integer rounding)).
I send this index factor to another VHDL file, which is used to calculate which value should be sent back. (ex: index = index_factor*step_counter)
When I get this index, I divide it by 100, and call sineLUT[index] to get the value that I need to generate a sine wave at the desired frequency.
The problem is, only the last 51 notes seem to work for me, and I do not know why. It seems to get stuck on a constant note at anything below that frequency (<650 hz) , and just decrease in volume every time I try to lower the note.
If you need parts of my code, let me know.
Just guessing, I suspect your step_counter isn't going through enough cycles, so your index (into the sine lut) doesn't go through a full 360 degrees for the lower frequencies.
For anything more helpful, you'll probably have to post code.
As an aside, why aren't you using something more like a conventional DDS? Analog Devices has a nice write-up on the basics: DDS Tutorial

Why are my accelerometer readings so slow?

Somewhere in the documentation they mentioned 400 Hz. Nice figure, but I end up getting something less than 100. Even on the latest, coolest, most awesome iPhone 4. And I'm not doing anything except incrementing a counter (ivar) and assigning the value to a label. Can't imagine this is the bottleneck.
I set the frequency to the maximum possible (very small number, like 1.0/10000). It is supposed to be capped to the max whatever the hardware supports.
You can adjust that frequency - see my answer.

Measuring Frequency of Square wave in MATLAB using USB 1024HLS

I'm trying to measure the frequency of a square wave which is read through a USB 1024 HLS Daq module through MATLAB. What I've done is create a loop which reads 100 values from the digitial input and that gives me vector of 0's and 1's. There is also a timer in this loop which measures the duration for which the loop runs.
After getting the vector, I then count the number of 1's and then use frequency = num_transitions/time to give me the frequency. However, this doesn't seem to work well :( I keep getting different frequencies for different number of iterations of the loop. Any suggestions?
I would suggest trying the following code:
vec = ...(the 100-element vector of digital values)...
dur = ...(the time required to collect the above vector)...
edges = find(diff(vec)); % Finds the indices of transitions between 0 and 1
period = 2*mean(diff(edges)); % Finds the mean period, in number of samples
frequency = 100/(dur*period);
First, the code finds the indices of the transitions from 0 to 1 or 1 to 0. Next, the differences between these indices are computed and averaged, giving the average duration (in number of samples) for the lengths of zeroes and ones. Multiplying this number by two then gives the average period (in number of samples) of the square wave. This number is then multiplied by dur/100 to get the period in whatever the time units of dur are (i.e. seconds, milliseconds, etc.). Taking the reciprocal then gives the average frequency.
One additional caveat: in order to get a good estimate of the frequency, you might have to make sure the 100 samples you collect contain at least a few repeated periods.
Functions of interest used above: DIFF, FIND, MEAN
First of all, you have to make sure that your 100 samples contain at least one full period of the signal, otherwise you'll get false results. You need a good compromise of sample rate (i.e. the more samples per period you have the better the measurement is) and and number of samples.
To be really precise, you should either have a timestamp associated with every measurement (as you usually can't be sure that you get equidistant time spacing in the for loop) or perhaps it's possible to switch your USB module in some "running" mode which doesn't only get one sample at a time but a complete waveform with fixed samplerate.
Concerning the calculation of the frequency, gnovice already pointed out the right way. If you have individual timestamps (in seconds), the following changes are necessary:
tst = ...(the timestamps associated with every sample)...
period = 2*mean(diff(tst(edges)));
frequency = 1/period;
I can't figure out the problem, but if the boolean vector were v then,
frequency = sum(v)/time_to_give_me_the_frequency
Based on your description, it doesn't sound like a problem with the software, UNLESS you are using the Windows system timer, which is notoriously inaccurate (it is only accurate to about 15 milliseconds).
There are high-resolution timers available in Windows, but I don't know how to use them in Matlab. If you have access to the .NET framework, the Stopwatch class has 1 microsecond accuracy (or better), as does the QueryPerformanceCounter API in Win32.
Other than that, you might have some jitter. There could be something in your signal chain that is causing false triggers, etc.
UPDATE: The following CodeProject article should solve the timing problem, if there is one. You should check the Matlab documentation of your version of Matlab to see if it has a native high-resolution timer. Otherwise, you can use this:
C++/Mex wrapper adds microsecond resolution timer to Matlab under WinXP
http://www.codeproject.com/KB/cpp/Matlab_Microsecond_Timer.aspx
mersenne31:
Thanks everyone for your responses. I have tried the solutions that gnovice and groovingandi mentioned and I'm sure they will work as soon as the timing issue is solved.
The code I've used is shown below:
for i=1:100 tic; value = getvalue(portCH); vector(i) = value(1); tst(i) = toc; % gets an individual time sample end
% to get the total time I put total_time = toc after the for loop
totaltime = sum(tst); edges = find(diff(vec)); % Finds the indices of transitions between 0 and 1 period = 2*mean(diff(edges)); % Finds the mean period, in number of samples frequency = 100/(totaltime*period);
The problem is that measuring the time for one sample doesn't really help because it is nearly the same for all samples. What is needed is, as groovingandi mentioned, some "running" mode which reads 100 samples for 3 seconds.
So something like for(3 seconds) and then we do the data capture. But I can't find anything like this. Is there any function in MATLAB that could do this?
This won't answer your question, but it's what I thought of after reading you question. square waves have infinite frequency. The FFT of a square wave it sin(x)/x, which goes from -inf to +inf.
Also try counting only the rising edges in matlab. You can quantize the signal to just +1 and 0, and then only increment the count when you see [0 1] slice of your vector.
OR
You can quantize, decimate, then just sum. This will only work if the each square pulse is the same length and your sampling frequency is constant. I think this one would be harder to do.