liamappelbe / fftea

A simple and efficient FFT implementation for Dart
Apache License 2.0
63 stars 1 forks source link

how to convert to decibel #22

Closed tawat25 closed 1 month ago

tawat25 commented 2 years ago

Hi I have used FFT(); like this ... final fftx = FFT(audio.length); final freqlist = fftx.realFft(audio); ... that result is real and imagine number How i convert to Decibel ?

liamappelbe commented 2 years ago

Use the magnitudes function to get the amplitudes. Since you're doing real FFT, you probably don't care about the redundant upper half of the frequencies, so you should also use discardConjugates:

final fftx = FFT(audio.length);
final freqlist = fftx.realFft(audio).discardConjugates().magnitudes();

That will give you the amplitudes.

Now, you said you wanted the decibels. Decibels is a logarithmic scale. If you're sure that's what you want, you can use this formula to convert the amplitude to decibels: 20 * log10(amplitude):

final decibels = freqlist.map(amp => 20 * math.log(amp) / math.ln10).toList();

If you want to know which frequency a particular entry in decibels corresponds to, use the frequency function:

for (int i = 0; i < decibels.length; ++i) {
  final freq = fftx.frequency(i, sampleRate);
  print("$freq Hz: ${decibels[i]} dB");
}

Where sampleRate is the sampling rate of your input audio (probably 44100Hz or 48000Hz or something like that).

tawat25 commented 2 years ago

I have run code on console mode is ok , I try to run on android but not finished yet .Are you ever implement this library with android ?

liamappelbe commented 2 years ago

It is implemented on android. It's probably taking a long time because your phone is slower than your PC.

What are you using it for? How long is your audio file?

You might want to use STFT instead. That will run a bunch of small FFTs, rather than one big one, and is much faster for long audio files. https://pub.dev/documentation/fftea/latest/stft/STFT-class.html

zjjt commented 1 year ago

Hi @liamappelbe , i have a kinda similar issue. i am trying to detect pauses or silence within an audio and act accordingly depending on the result. The audio files are mp3s and are for voice reading. How can i detect those and their position within the track ?

liamappelbe commented 1 year ago

i am trying to detect pauses or silence within an audio

@zjjt You don't need FFT to do that. Try just iterating through the audio samples, and if the value stays below some threshold for some amount of time, that's silence.

For more robust results, you can calculate a moving average of the absolute value (or maybe the squared value) of the samples.

zjjt commented 1 year ago

@liamappelbe oh ...im struggling to find à way to do this in dart and thought that fftea could help me out. Any ideas how can I access the samples data to start ?

liamappelbe commented 1 year ago

@zjjt Whether or not you use fftea, you'll need to load the MP3s and get the audio samples. That's a separate problem.

flutter_lame looks like it can read MP3 files, but it seems to only work on Flutter. If you're able to convert the MP3s to WAV files, then you can use package:wav (another of my packages 😉) to get the samples.

zjjt commented 1 year ago

@liamappelbe you are the one Who have written the wav package? Cool ...thanks for the tips ill see how I can convert my files to wav I guess it ll be better...For now i load my files feom the assetbundle and use audioplayers package ti olay them.ok let me revert when I am done