Home > utility > lookupTableWrite.m

lookupTableWrite

PURPOSE ^

lookupTableWrite: Write a lookup table to a file

SYNOPSIS ^

function lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt)

DESCRIPTION ^

lookupTableWrite: Write a lookup table to a file
    Usage: lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt)
    所給定的查表函數必須是嚴格遞增(reverse=0)或是嚴格遞減(reverse=1)。
    所產生的 inc 檔案會被用在 tableLookupByRangeSearch in utility.cpp

    For example, to generate the table for mixLogSum:
        tableFunName='mixLogSum';
        tableInvFunName='mixLogSumInv';
        xRange=[0, 25];
        xySf=[2^15, 2^15];
        tableName='mixLogSumTable';
        plotOpt=1;
        lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt);

    Another example, to generate the table for log:
        tableFunName='log';
        tableInvFunName='exp';
        xRange=[0.1, 5];
        xySf=[2^10, 2^10];
        tableName='logTable';
        plotOpt=1;
        lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt);

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt)
0002 %lookupTableWrite: Write a lookup table to a file
0003 %    Usage: lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt)
0004 %    所給定的查表函數必須是嚴格遞增(reverse=0)或是嚴格遞減(reverse=1)。
0005 %    所產生的 inc 檔案會被用在 tableLookupByRangeSearch in utility.cpp
0006 %
0007 %    For example, to generate the table for mixLogSum:
0008 %        tableFunName='mixLogSum';
0009 %        tableInvFunName='mixLogSumInv';
0010 %        xRange=[0, 25];
0011 %        xySf=[2^15, 2^15];
0012 %        tableName='mixLogSumTable';
0013 %        plotOpt=1;
0014 %        lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt);
0015 %
0016 %    Another example, to generate the table for log:
0017 %        tableFunName='log';
0018 %        tableInvFunName='exp';
0019 %        xRange=[0.1, 5];
0020 %        xySf=[2^10, 2^10];
0021 %        tableName='logTable';
0022 %        plotOpt=1;
0023 %        lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt);
0024 
0025 %    Roger Jang, 20070811
0026 
0027 if nargin<1, selfdemo; return; end
0028 
0029 tableName=upper(tableName);
0030 
0031 % ====== Define the constants of expTable
0032 xMinOrig=xRange(1); xMaxOrig=xRange(2);
0033 xSf=xySf(1); ySf=xySf(2);        % Scaling factors on x and y
0034 x=[xMinOrig, xMaxOrig];
0035 table=feval(tableFunName, x);        % 只是用來找 yMin 和 yMax
0036 yMin=round(ySf*min(table)); yMax=round(ySf*max(table));
0037 xMin=round(xMinOrig*xSf); xMax=round(xMaxOrig*xSf);
0038 % 反運算以找回x的值
0039 tableInt=yMin:yMax;
0040 x2=feval(tableInvFunName, (tableInt+0.5)/ySf);     % x 的右界值: x in [x(i-1), x(i)] ===> log(1+exp(x)) = y(i)
0041 x2=x2(1:end-1);                    % 刪除最後一個。如果 table 的長度是 n,x 的長度就是 n-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 % 為了進行 binary range search, xInt 必須是由小排到大
0049 reverse=0;
0050 if xInt(1)>xInt(end)
0051     xInt=fliplr(xInt);
0052     reverse=1;
0053 end
0054 
0055 % ====== Write table related constants
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 % ====== Write table entries
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 % ====== Write the txt content of table
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 % ====== Write the bin content of table
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 % ====== Self demo
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 %feval(mfilename, tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt);
0112 lookupTableWrite(tableFunName, tableInvFunName, xRange, xySf, tableName, plotOpt);

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003