DSPRelated.com
Code

double2cfloat.m - TMX Filter formatting for C compilers

David Valencia February 2, 2011 Coded in Matlab

This is required for the TMX Program in MATLAB and C versions, please check the following snippets for more information:

MATLAB VERSION: http://www.dsprelated.com/showcode/75.php

C VERSION: Coming soon!

An example of the result can be found in the following snippet:

http://www.dsprelated.com/showcode/73.php

 

% UPIITA IPN 2010
% José David Valencia Pesqueira

% This program is useful to convert numbers in MATLAB's Double
% format to a text file in float format for C compilers

% You should copy the resulting text to a file and save it with .h
% extension

% THIS PROGRAM RECEIVES AN UNIDIMENTIONAL VECTOR OF DOUBLE NUMBERS

% close all; clear all; clc;

function double2cfloat(x , namex, y, namey, sinc_factor)
[m,n] = size(x);
fprintf('// Begin of file\n\n');
fprintf('#define sinc %d // Sincronization factor\n',sinc_factor);
fprintf('#define N %d // Filter length\n',n);
if m~=0
    fprintf('#define M %d // Number of filters(branches)\n\n',m);
    fprintf('float %s[M][N]=',namex);    
else
    fprintf('float %s[N]=',namex);
end
disp('');
disp('{');
for i=0:m-1 %Rows
    if m~=1
        disp('{');
    end
    for j=0:n-1 %columns
        fprintf('%1.4e,\n',x(i+1,j+1));
    end
    if m~=1 
        fprintf('},\n');
    end
end
disp('};');
fprintf('\n\n');

if m~=0
    fprintf('float %s[M][N]=',namey);    
else
    fprintf('float %s[N]=',namey);
end
disp('');
disp('{');
for i=0:m-1 %Rows
    if m~=1
        disp('{');
    end
    for j=0:n-1 %columns
        fprintf('%1.4e,\n',y(i+1,j+1));
    end
    if m~=1 
        fprintf('},\n');
    end
end
disp('};');

fprintf('\n// EOF\n\n');