0
$\begingroup$

This is probably a very simple Signal Processing question but I can't quite work it out. I'm currently writing an android app which needs to perform an fft on audio data taken from the phone's microphone. I'm currently reading in the audio data into a byte array buffer at a sampling frequency of 44100Hz.

 int bufferSize = AudioRecord.getMinBufferSize(44100,
                        AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);

                final AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
                        AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT,
                        bufferSize);

                final AudioTrack audioTrack = new AudioTrack(
                        AudioManager.STREAM_MUSIC, 44100,
                        AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT,
                        bufferSize, AudioTrack.MODE_STREAM);

                final byte[] buffer = new byte[bufferSize];
                final int finalBufferSize = bufferSize;

Once the buffer has been filled I convert the data to an array of Complex numbers and pass the data into an fft function. My problem is that the fft function I have cannot work with 44100 samples as it requires the length of the array of samples to be a power of 2. I've found what I think are some solutions online but I'm a little unsure if I'm fully understanding them correctly.

As I understand it I can either pad the array out with zeroes in order to make the array length a power of 2 or I can take a fixed fft size, e.g. 1024 samples. If I use the second solution do I need to divide the original array of samples into 1024 chunks and run an fft on each? Any advice on how I should go about this would be appreciated.

Here is the fft class I'm using: http://introcs.cs.princeton.edu/java/97data/FFT.java.html

$\endgroup$
  • 1
    $\begingroup$ why do you need an FFT of 44100 samples? you don't explain that. $\endgroup$ – Marcus Müller Jul 4 '16 at 15:27
  • $\begingroup$ As in why do I use 44100 or why am I doing the fft? As I understand it sound is usually sampled at 44100. I need the fft because the app needs to determine the pitch of musical notes. $\endgroup$ – PyroPez Jul 4 '16 at 15:34
  • 1
    $\begingroup$ yes, but why 44100? $\endgroup$ – Marcus Müller Jul 4 '16 at 15:46
  • 1
    $\begingroup$ The FFT doesn't care at all at what rate you sampled something – the FFT is just an implementation of the DFT, which really just maps vectors of N samples to other vectors of N samples. It's a linear operation. The question what the elements of the resulting vector signify is something that determines the FFT length. $\endgroup$ – Marcus Müller Jul 4 '16 at 16:00
  • 1
    $\begingroup$ also, by the way, don't use native Java code to do an FFT, if you can avoid it. The FFTw runs perfectly fine on Android on x86 and ARM, and is bound to be orders of magnitudes faster, and will also support very fast FFTs for non-power-of-2 FFT lengths. $\endgroup$ – Marcus Müller Jul 4 '16 at 16:01
1
$\begingroup$

A 44100 Hz sample rate just means that you get 1 sample approximately every 22.67 microseconds. It also means you get N samples in N*22.67 microseconds, and you're free to choose N. If you choose N=44100, you get 44100 samples in one second. If you choose N=1024, you get 1024 samples in 23.22 milliseconds.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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