Code covered by the BSD License  

Highlights from
Echo Canceller Workbook targeting a dsPIC

image thumbnail

Echo Canceller Workbook targeting a dsPIC

by

 

22 Jan 2008 (Updated )

This set of models and word documents show an echo canceller development workflow.

to_header.m
% Convert simulation data produced by a Simulink model and create a C
% header file which can be used by MPLAB for the dsPIC30F6014A.
% The vars Frame and logsout should be defined by the Simulink model ec_fixed1.mdl
% or a variant thereof.
% You'll need to edit the structure if you change the name of the subsystem
% via Edit/Find&Replace.
%   Copyright 2008 The MathWorks, Inc.

num_frames_to_process = 10;
shift_factor = 14;  % set to the precision of your data, e.g. 14 if sfix16_en14.
output_subdir = '\ec_ert_rtw\';
top_struc = logsout.Echo_Canceller; % this will change depending on SL implementation
% pic_frame_size; % should be defined in workspace already, size is 80 for
% dsPIC work

% extract a portion of the overall data set if memory is lacking (AND IT IS) in
% the processor where the data will be eventually loaded
start = 1;
stop = pic_frame_size*num_frames_to_process; % pic_frame_size should be defined by the Simulink model
disp(['Using ',num2str(stop-start+1),' samples from overall record if permitting']);
% extract the data of interest from the logsout structure

%len = length(logsout.sin_linear.Data);
len = length(top_struc.sin_linear.Data);

if (len < (stop-start))
  error('Length of data is too long for specified start and stop indices');
end

%send_in = logsout.sin_linear.Data(start:stop);
%rcv_in = logsout.rin_linear.Data(start:stop);
%send_out = logsout.sout_linear.Data(start:stop);

send_in = top_struc.sin_linear.Data(start:stop);
rcv_in = top_struc.rin_linear.Data(start:stop);
%send_out = top_struc.sout_linear.Data(start:stop); % no need to save

% convert it to integer from fixed point
% there should be no fractional part when done
send_in_int16 = double(send_in) * 2^(shift_factor);
rcv_in_int16 = double(rcv_in) * 2^(shift_factor);
%send_out_int16 = double(send_out) * 2^(shift_factor);  % no need to save
% save to an ascii text file
save send_in.txt send_in_int16 -ascii;
save rcv_in.txt rcv_in_int16 -ascii;
%save send_out.txt send_out_int16 -ascii;  % no need to save

lf = char(10); % line feed character
disp('Running...');
% these files are the output of running the simulink simulation
% ec_fixed.mdl or some variant thereof.
text_file_dir = pwd;
text_files = {'rcv_in','send_in'};
% the received input array feeding back from speaker into microphone
% and the send input array which is the desired transmit voice
var_name = {'rcv_in_record','send_in_record'};
for file_index = 1:2    
    % Open file for reading
    fid_in = fopen([pwd,'\',text_files{file_index},'.txt'],'rt');
    % Read in the contents to a data array
    [data,count] = fread(fid_in,'uchar');
    % Convert to characters
    data = char(data);
    % Open the output file for writing
    fid_out = fopen([pwd,output_subdir,text_files{file_index},'.h'],'wt');
    % Pre-allocat an array for the new data, the output
    newdata = zeros(1,10000);
    % Start parsing the input file and building the new data array, the output
    j = 1;
    line_counter = 0;
    for i = 1:count
      if data(i) == lf
        newdata(j) = ',';j=j+1;
        newdata(j) = lf;j=j+1;
      else
        newdata(j) = data(i);j=j+1;
      end;
    end;
    newdata = newdata(1:j-3);  % remove the last comma
    newdata = [newdata,lf]; 
    % write out the secret code to avoid deletion by RTW build process
    fwrite(fid_out,['/* COMPANY-NAME target specific file',lf],'char');
    fwrite(fid_out,['*',lf],'char');
    fwrite(fid_out,['* This file is created for use with the',lf],'char');
    fwrite(fid_out,['* COMPANY-NAME target.',lf],'char');
    fwrite(fid_out,['* It is used for ...',lf],'char');
    fwrite(fid_out,['*/',lf],'char');
    % then write out the important stuff, the data to be imported into the
    % program as a test vector.
    fwrite(fid_out,['/* Header file created by ',mfilename,'.m */',lf],'char');
    fwrite(fid_out,['/* ',num2str(clock),' */',lf],'char');
    fwrite(fid_out,['const int ',var_name{file_index},'[]={',lf],'char');
    fwrite(fid_out,newdata,'char');
    fwrite(fid_out,[lf,'};',lf],'char');
    fclose(fid_out);
end
disp('Done.');

Contact us