Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global lu_exp;
- global lu_log;
- function initLookups()
- disp("Initializing lookup tables");
- global lu_exp;
- lu_exp = zeros(1,256,"int32");
- global lu_log;
- lu_log = zeros(1,256,"int32");
- for i=0:255
- lu_log(i+1) = round(-log2( sin((double(i)+0.5)*pi/2/256) )*256);
- lu_exp(i+1) = round((power(2, double(i)/256)-1)*1024);
- endfor
- endfunction
- initLookups();
- function y=eMap(x)
- eff = bitand(x,255);
- if(bitand(x, 256) == 0)
- y = eff;
- else
- y = bitxor(eff, 255);
- endif
- endfunction
- function y=eExp(val)
- global lu_exp;
- y = lu_exp(bitand(val,255)+1);
- # now reverse calculate the floating point value...
- y = bitshift(1024+y, bitshift(val,-8));
- endfunction
- function y=eLog(phase)
- global lu_log;
- y = lu_log(eMap(phase)+1);
- endfunction
- function y=eSin(amp, phase)
- phase = bitand(phase, 1023);
- amp = bitshift(amp-64, 4);
- y = eExp(amp-eLog(phase));
- if(bitand(phase,512)!=0)
- y = -y;
- endif
- endfunction
- function y=eSinV(amp,phase,vamp,vphase)
- y = eSin(amp, phase+eSin(vamp,vphase));
- endfunction
- function y = sintest(phase)
- #y = eSinV(63, phase, 16, phase/3);
- y = eSin(0, phase);
- endfunction
- x = 0:1023;
- sl = arrayfun(@eLog, x);
- se = arrayfun(@eExp, x);
- st = arrayfun(@sintest, x);
- plot(x, st, ";Sin;");
- #plot(x, sl, ";Logarithmic lookup;", x, se, ";Exponential lookup;", x, st, ";Calculated sine wave;");#, x, abs(((st.-16384*sin(x./1024*pi*2)).*10000./st)), "--;Rel. error to real sine (x10000);");
- title("OPL2 Sine wave");
- xlabel("Phase (0-1023)");
- ylabel("Value (Amplitude = 256)");
- grid;
Advertisement
Add Comment
Please, Sign In to add comment