%Copyright (C) 2006 Kenneth Jensen
%
%This program is free software; you can redistribute it and/or
%modify it under the terms of the GNU General Public License
%as published by the Free Software Foundation; either version 2
%of the License, or (at your option) any later version.
%
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%GNU General Public License for more details.
%
%You should have received a copy of the GNU General Public License
%along with this program; if not, write to the Free Software
%Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

% file: calcCoord.m
% date: 2005-01-28
% version: 0.1
% author: kenny jensen <kjensen@alum.mit.edu>
% description: Converts a byteNum (the number of userdata bytes since the
%    beginning of the cd) to a set of cartesian and polar coordinates
% input:
%    byteNum - [(sector 1, byte 16), ..., (sector 1, byte 2351), 
%            (sector 2, byte 16), ..., (sector 2, byte 2351), ...]
%    cdprop - [linearVelocity, trackPitch, centralRadius]
% notes:  I think that there is something wrong with my CIRC encoder :(
function [x,y,r,theta] = calcCoord(byteNum, cdprop)

if nargin == 1
  % default cd properties if none are entered
  vel = 1.2e3; %mm/s
  delta = 1.5e-3; %mm
  r1 = 25; %mm
elseif nargin == 2
  vel = cdprop(1);
  delta = cdprop(2);
  r1 = cdprop(3);
end;


% convert byteNum to [channelFrame, channelBitNum]
% note each channelFrame = 136us

% byteNum to [sectorNum, sectorByteNum]
sectorNum = floor((byteNum-1)/2336)+1;
sectorByteNum = mod((byteNum-1),2336)+17; % 20050124, bug#1: 16+ for sync bytes, tested this and it
					  % doesn't seem to affect the image...?

% [sectorNum, sectorByteNum] to [f1Frame, f1ByteNum]
% [1-, 1-2352]
% note: there are 98 frames per sector, 24 bytes per frame
% the bytes in the frame are even-odd flipped: [1 2 3 4] -> [2 1 4 3]
f1Frame = (sectorNum-1)*98+floor((sectorByteNum-1)/24)+1;
f1ByteNum = mod((sectorByteNum-1),24)+2*mod(sectorByteNum,2);

% [f1Frame, f1ByteNum] to [f2Frame, f2ByteNum]
% [1-, 1-24]
f1tof2Frame = [3, 6, 27, 30, 65, 68, 89, 92, 11, 14, 35, 38, 73, ...
	       76, 97, 100, 19, 22, 43, 46, 81, 84, 105, 108]-3;
f1tof2ByteNum = [1, 2, 7, 8, 17, 18, 23, 24, 3, 4, 9, 10, ...
		 19, 20, 25, 26, 5, 6, 11, 12, 21, 22, 27, 28];

f2Frame = f1tof2Frame(f1ByteNum)+f1Frame;
f2ByteNum = f1tof2ByteNum(f1ByteNum);

% [f2Frame, f2ByteNum] to [f3Frame, f3ByteNum]
% [1-, 1-32]
f3Frame = f2Frame;
f3ByteNum = f2ByteNum+1; %adds control byte at beginning

% f3ByteNum to channelBitNum 
% (f3 control byte -> f3ByteNum = 0)
channelFrame = f3Frame;
channelBitNum = f3ByteNum*(14+3)+27;

L = (channelFrame+(channelBitNum-1)/588)*vel*136e-6;

numWind = floor(-(r1/delta)*(1-sqrt(1+L*delta/(pi*r1^2))));

r = r1+numWind*delta;
theta = mod(-(L-(pi*delta*numWind.^2+(2*pi*r1)*numWind))./r,2*pi);

x = r.*cos(theta);
y = r.*sin(theta);