Guest User

OPL2 PG

a guest
Mar 15th, 2011
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.53 KB | None | 0 0
  1. global lu_exp;
  2. global lu_log;
  3. function initLookups()
  4.     disp("Initializing lookup tables");
  5.     global lu_exp;
  6.     lu_exp = zeros(1,256,"int32");
  7.     global lu_log;
  8.     lu_log = zeros(1,256,"int32");
  9.     for i=0:255
  10.         lu_log(i+1) = round(-log2( sin((double(i)+0.5)*pi/2/256) )*256);
  11.         lu_exp(i+1) = round((power(2, double(i)/256)-1)*1024);
  12.     endfor
  13. endfunction
  14. initLookups();
  15.  
  16. function y=eMap(x)
  17.     eff = bitand(x,255);
  18.     if(bitand(x, 256) == 0)
  19.         y = eff;
  20.     else
  21.         y = bitxor(eff, 255);
  22.     endif
  23. endfunction
  24. function y=eExp(val)
  25.     global lu_exp;
  26.     y = lu_exp(bitand(val,255)+1);
  27.     # now reverse calculate the floating point value...
  28.     y = bitshift(1024+y, bitshift(val,-8));
  29. endfunction
  30. function y=eLog(phase)
  31.     global lu_log;
  32.     y = lu_log(eMap(phase)+1);
  33. endfunction
  34. function y=eSin(amp, phase)
  35.     phase = bitand(phase, 1023);
  36.     amp = bitshift(amp-64, 4);
  37.     y = eExp(amp-eLog(phase));
  38.     if(bitand(phase,512)!=0)
  39.         y = -y;
  40.     endif
  41. endfunction
  42. function y=eSinV(amp,phase,vamp,vphase)
  43.     y = eSin(amp, phase+eSin(vamp,vphase));
  44. endfunction
  45.  
  46. function y = sintest(phase)
  47.     #y = eSinV(63, phase, 16, phase/3);
  48.     y = eSin(0, phase);
  49. endfunction
  50.  
  51. x = 0:1023;
  52. sl = arrayfun(@eLog, x);
  53. se = arrayfun(@eExp, x);
  54. st = arrayfun(@sintest, x);
  55. plot(x, st, ";Sin;");
  56. #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);");
  57. title("OPL2 Sine wave");
  58. xlabel("Phase (0-1023)");
  59. ylabel("Value (Amplitude = 256)");
  60. grid;
Advertisement
Add Comment
Please, Sign In to add comment