0001 function lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 if nargin<1, selfdemo; return; end
0028
0029 tableName=upper(tableName);
0030
0031
0032 xMinOrig=xRange(1); xMaxOrig=xRange(2);
0033 xSf=xySf(1); ySf=xySf(2);
0034 x=[xMinOrig, xMaxOrig];
0035 table=feval(tableFunName, x);
0036 yMin=round(ySf*min(table)); yMax=round(ySf*max(table));
0037 xMin=round(xMinOrig*xSf); xMax=round(xMaxOrig*xSf);
0038
0039 tableInt=yMin:yMax;
0040 x2=feval(tableInvFunName, (tableInt+0.5)/ySf);
0041 x2=x2(1:end-1);
0042 xInt=round(x2*xSf);
0043 tableSize=length(xInt);
0044 if plotOpt
0045 plot(xInt, (tableInt(1:end-1)+tableInt(2:end))/2, '.-');
0046 end
0047
0048
0049 reverse=0;
0050 if xInt(1)>xInt(end)
0051 xInt=fliplr(xInt);
0052 reverse=1;
0053 end
0054
0055
0056 file=[tableName, 'VAR.h'];
0057 fid=fopen(file, 'w');
0058 fprintf(fid, '#ifndef %s_H\n', tableName);
0059 fprintf(fid, '#define %s_H\n', tableName);
0060 fprintf(fid, '\n');
0061 fprintf(fid, '#define %s_SIZE %d\n', tableName, tableSize);
0062 fprintf(fid, '#define %s_X_SF %d\n', tableName, xSf);
0063 fprintf(fid, '#define %s_Y_SF %d\n', tableName, ySf);
0064 fprintf(fid, '#define %s_X_MIN %d\n', tableName, xMin);
0065 fprintf(fid, '#define %s_X_MAX %d\n', tableName, xMax);
0066 fprintf(fid, '#define %s_X_MIN_ORIG %f\n', tableName, xMinOrig);
0067 fprintf(fid, '#define %s_X_MAX_ORIG %f\n', tableName, xMaxOrig);
0068 fprintf(fid, '#define %s_Y_MIN %d\n', tableName, yMin);
0069 fprintf(fid, '#define %s_Y_MAX %d\n', tableName, yMax);
0070 fprintf(fid, '#define %s_REVERSE %d\n', tableName, reverse);
0071 fprintf(fid, '\n');
0072 fprintf(fid, '#endif\n');
0073 fclose(fid);
0074 fprintf('Saved %s\n', file);
0075
0076
0077 file=[tableName, '.inc'];
0078 fid=fopen(file, 'w');
0079 fprintf(fid, 'int %s[%d]={%d', tableName, tableSize, xInt(1));
0080 for i=2:tableSize
0081 fprintf(fid, ', %d', xInt(i));
0082 end
0083 fprintf(fid, '};\n');
0084 fclose(fid);
0085 fprintf('Saved %s\n', file);
0086
0087 return
0088
0089
0090 file='tableInt.txt';
0091 fid=fopen(file, 'w');
0092 fprintf(fid, '%d\n', xInt);
0093 fclose(fid);
0094 fprintf('Saved %s\n', file);
0095
0096
0097 file='tableInt.bin';
0098 fid=fopen(file, 'wb');
0099 fwrite(fid, xInt, 'integer*4');
0100 fclose(fid);
0101 fprintf('Saved %s\n', file);
0102
0103
0104 function selfdemo
0105 tableFunName='mixLogSum';
0106 tableInvFunName='mixLogSumInv';
0107 xRange=[0, 25];
0108 xySf=[2^15, 2^15];
0109 tableName='mixLogSumTable';
0110 plotOpt=1;
0111
0112 lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt);