
%% Float prep
float_file = fopen("omasa_float_dump.bin", "rb");
floatdata_raw = fread(float_file, Inf, "float32");
fclose(float_file);

numbands = 24;
numbands_actual = 24;
numsf = length(floatdata_raw)/5/numbands;

azi_fl = zeros(numsf, numbands);
ele_fl = zeros(numsf, numbands);
ratio_fl = zeros(numsf, numbands);
spread_fl = zeros(numsf, numbands);
surcoh_fl = zeros(numsf, numbands);

for n = 1:numsf
    sfskip = (n-1)*5*numbands;
    azi_fl(n,:) = floatdata_raw((1:numbands)+sfskip);
    ele_fl(n,:) = floatdata_raw((1:numbands)+sfskip+numbands);
    ratio_fl(n,:) = floatdata_raw((1:numbands)+sfskip+2*numbands);
    spread_fl(n,:) = floatdata_raw((1:numbands)+sfskip+3*numbands);
    surcoh_fl(n,:) = floatdata_raw((1:numbands)+sfskip+4*numbands);
end

% Postprocess data for easier comparison

% Remove unused bands
azi_fl = azi_fl(:,1:numbands_actual);
ele_fl = ele_fl(:,1:numbands_actual);
ratio_fl = ratio_fl(:,1:numbands_actual);
spread_fl = spread_fl(:,1:numbands_actual);
surcoh_fl = surcoh_fl(:,1:numbands_actual);

% Copy first sf to all sf of frame for ratios
for n = 1:4:numsf
    ratio_fl(n+1,:) = ratio_fl(n,:);
    ratio_fl(n+2,:) = ratio_fl(n,:);
    ratio_fl(n+3,:) = ratio_fl(n,:);

    surcoh_fl(n+1,:) = surcoh_fl(n,:);
    surcoh_fl(n+2,:) = surcoh_fl(n,:);
    surcoh_fl(n+3,:) = surcoh_fl(n,:);
end

%% Fixed prep

fixed_file = fopen("omasa_fx_dump.bin", "rb");
fixeddata_raw = fread(fixed_file, Inf, "int32");
fclose(fixed_file);

numbands = 24;
numbands_actual = 24;
numsf = length(fixeddata_raw)/5/numbands;

azi_fx = zeros(numsf, numbands);
ele_fx = zeros(numsf, numbands);
ratio_fx = zeros(numsf, numbands);
spread_fx = zeros(numsf, numbands);
surcoh_fx = zeros(numsf, numbands);

for n = 1:numsf
    sfskip = (n-1)*5*numbands;
    azi_fx(n,:) = fixeddata_raw((1:numbands)+sfskip);
    ele_fx(n,:) = fixeddata_raw((1:numbands)+sfskip+numbands);
    ratio_fx(n,:) = fixeddata_raw((1:numbands)+sfskip+2*numbands);
    spread_fx(n,:) = fixeddata_raw((1:numbands)+sfskip+3*numbands);
    surcoh_fx(n,:) = fixeddata_raw((1:numbands)+sfskip+4*numbands);
end


% Postprocess data for easier comparison

% Remove unused bands
azi_fx = azi_fx(:,1:numbands_actual);
ele_fx = ele_fx(:,1:numbands_actual);
ratio_fx = ratio_fx(:,1:numbands_actual);
spread_fx = spread_fx(:,1:numbands_actual);
surcoh_fx = surcoh_fx(:,1:numbands_actual);

% Copy first sf to all sf of frame for ratios
for n = 1:4:numsf
    ratio_fx(n+1,:) = ratio_fx(n,:);
    ratio_fx(n+2,:) = ratio_fx(n,:);
    ratio_fx(n+3,:) = ratio_fx(n,:);

    surcoh_fx(n+1,:) = surcoh_fx(n,:);
    surcoh_fx(n+2,:) = surcoh_fx(n,:);
    surcoh_fx(n+3,:) = surcoh_fx(n,:);
end


%% Comparison

% Convert float to fixed
azi_fl_infx = round(azi_fl.*2^22); % azi Q22
ele_fl_infx = round(ele_fl.*2^22); % ele Q22
ratio_fl_infx = round(ratio_fl.*2^30); % ratio Q30
spread_fl_infx = round(spread_fl.*2^30); % spread Q30
surcoh_fl_infx = round(surcoh_fl.*2^31); % surcoh Q31

azi_diff = (azi_fx - azi_fl_infx)./(2^22);
ele_diff = (ele_fx - ele_fl_infx)./(2^22);
ratio_diff = (ratio_fx - ratio_fl_infx)./(2^30);
spread_diff = (spread_fx - spread_fl_infx)./(2^30);
surcoh_diff = (surcoh_fx - surcoh_fl_infx)./(2^31);

% Sanitize azi_diff to range [-180,+180)
azi_diff = azi_diff - 360*(azi_diff >= 180);
azi_diff = azi_diff + 360*(azi_diff < -180);


%%
figure(1);
subplot(3,1,1)
plotspec(1:numsf, 1:numbands_actual, azi_diff', [1 numsf 1 numbands_actual+1], [-180,180], 'Subframes', 'Bands', 'Azi diff');

subplot(3,1,2)
plotspec(1:numsf, 1:numbands_actual, ele_diff', [1 numsf 1 numbands_actual+1], [-180,180], 'Subframes', 'Bands', 'Ele diff');

subplot(3,1,3)
plotspec(1:numsf, 1:numbands_actual, ratio_diff', [1 numsf 1 numbands_actual+1], [-1,1], 'Subframes', 'Bands', 'Ratio diff');

% subplot(5,1,4)
% plotspec(1:numsf, 1:numbands_actual, spread_diff', [1 numsf 1 numbands_actual+1], [-1,1], 'Subframes', 'Bands', 'Spread diff');
% 
% subplot(5,1,5)
% plotspec(1:numsf, 1:numbands_actual, surcoh_diff', [1 numsf 1 numbands_actual+1], [-1,1], 'Subframes', 'Bands', 'Surcoh diff');



function plotspec(t, f, datatoplot, axlimits, caxis_values, xtext, ytext, ttext)


customcolor = zeros(256,3);
customcolor(1:128,1) = linspace(1,0,128).^0.7;
customcolor(129:256,2) = linspace(0,1,128).^0.7;

f = [f, f(end)+1];
datatoplot = [datatoplot; datatoplot(end,:)];

surf(t, f, datatoplot)
view(2)
shading flat
axis(axlimits)
colormap(customcolor)
clim(caxis_values)
colorbar
xlabel(xtext);
ylabel(ytext);
title(ttext);

end


