I have to extract a particular frequency range from an audio signal. For that I am using an FIR filter of order 50 and it is written in MATLAB as below:

[input fs nbits]=wavread('female_speech.wav');
b1=fir1(order,[0.1 0.2]);  
input_buffer=zeros(1,len);%%variable used to store incoming input samples
filter_input=zeros(1,order+1);%%input to the BPF which is equal to the order of BPF
for iteration=1:len 
    channel1_op(1,iteration)= b1*filter_input';

Here I am giving a .wav file as input and it is stored as an array and the processing in done for the entire samples. But if I want to implement in real time in MATLAB, i.e to input audio from the microphone connected to PC and then process that data in real time, what all things have to be considered regarding the coding since in real time the length of the data is not known before


1 Answer 1


For realtime processing you typically need to rewrite your code to work on finite buffers of length N. Often, this will include «patching up» to correctly transfer information across block boundaries eg using overlap-add.

I notice that you are defining your filter from ground up. For efficiency you typically want to use the built-in filter() or conv() functions, particularly in a realtime application. These are typically implemented in C or assembly using whatever tricks to make the blistering fast. filter() conveniently offers input and output of initial conditions/tail, and if you have the signal processing toolbox you also get the filtic() function for convenience.

For actually processing realtime audio in Matlab, I believe that you need extra functionality. I have some experience with playrec based on portaudio that is crossplatform and free, but not really maintained. Last I tried, it could be brought up to speed (be aware that MacOS will happily change APIs around, introduce security changes and «force» you to use the latest-ish OS and the lastest Matlab revision for stuff to work. That makes it painful to use sparingly updated libraries compared to Windows or Linux)


The other alternative is to buy Mathworks audiotoolbox. I did not test its realtime processing but I assume that it works.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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