www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/ProcessAudioDataUsingOnlyMATLABFunctionsCodeExample.m

    %% Process Audio Data Using Only MATLAB Functions Code  
% This example shows how to write MATLAB(R) function-only code for reading
% audio data. 
%
% The code reads audio data from a file, filters it, and then plays the
% filtered audio data. The audio data is read in frames. This code produces
% the same result as the System objects code in the next example, allowing
% you to compare approaches.   

%% 
% Locate source audio file. 
fname = 'speech_dft_8kHz.wav';  

%% 
% Obtain the total number of samples and the sampling rate from the source
% file. 
audioInfo = audioinfo(fname);
maxSamples = audioInfo.TotalSamples;
fs = audioInfo.SampleRate;  

%% 
% Define the filter to use. 
b = fir1(160,.15);  

%% 
% Initialize the filter states. 
z = zeros(1,numel(b)-1);  

%% 
% Define the amount of audio data to process at one time, and initialize
% the while loop index. 
frameSize = 1024; 
nIdx = 1;  

%% 
% Define the while loop to process the audio data in |processloop|. 
type processloop
%% 
% The loop uses explicit indexing and state management, which can be a tedious
% and error-prone approach. You must have detailed knowledge of the states,
% such as, sizes and data types. Another issue with this MATLAB-only code
% is that the |sound| function is not designed to run in real time. The
% resulting audio is very choppy and barely audible.