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

    %% Audio System Design with System Objects
% This example shows how to create a system that processes a long stream of
% audio data using System objects. System objects are components you can use
% to create your system in MATLAB. The data is read from a file, filtered, and 
% then played.  
%% Create Components for Your System
% This section of the example shows how to set up your system. The predefined
% components you need are: 
%  
% * |dsp.AudioFileReader| — Read the file of audio data  
% * |dsp.FIRFilter| — Filter the audio data  
% * |audioDeviceWriter| — Play the filtered audio data    
%
% First, create the component objects, using default property settings. 
audioIn = dsp.AudioFileReader;
filtLP = dsp.FIRFilter;
audioOut = audioDeviceWriter;  
%% Configure Component Property Values  
% This section of the example shows how to configure the components for 
% your system by setting the component objects’ properties. 
%
% Use this procedure if you have created your components separately from
% configuring them. You can also create and configure your components
% at the same time, as described in a later example. 
% 
% For the file reader object, specify the file to read and set the output
% data type. 
% 
% For the filter object, specify the filter numerator coefficients using
% the |fir1| function, which specifies the lowpass filter order and the
% cutoff frequency. 
%
% For the audio device writer object, specify the sample rate. In this case,
% use the same sample rate as the input data. 
audioIn.Filename = 'speech_dft_8kHz.wav';
audioIn.OutputDataType = 'single';  
filtLP.Numerator = fir1(160,.15);  
audioOut.SampleRate = audioIn.SampleRate;    
%% Connect Components in a System  
% This section shows how to connect the components together to read, filter,
% and play a file of audio data. The while loop uses the |isDone| method to
% read through the entire file.   
while ~isDone(audioIn)
    audio = audioIn();    % Read audio source file
    y = filtLP(audio);        % Filter the data
    audioOut(y);              % Play the filtered data
end
%% Change a Tunable Property in Your System  
% You can change the filter type to a high-pass filter as your code is running
% by modifying the |Numerator| property of the filter object. The change
% takes effect at the next iteration of the while loop. 
reset(audioIn);               % Reset audio file
filtLP.Numerator = fir1(160,0.15,'high'); 
while ~isDone(audioIn)
    audio = audioIn();    % Read audio source file
    y = filtLP(audio);    % Filter the data
    audioOut(y);          % Play the filtered data
end