%-------------------------------------------------------------------------------
%
%  David S. De Lorenzo
%  July 14, 2002
%
%  IMPORTANT:  ONLY CURVE FIT FOR IF GAIN >= 20
%
%-------------------------------------------------------------------------------
%
%  When RF Gain is set to 14:
%     IF Gain = 20    IF Gain = 47
%     -17.156 dBm      +7.388 dBm
%
%  When RF Gain is set to 0:
%     IF Gain = 20    IF Gain = 47
%     -32.442 dBm      -6.346 dBm
%
%-------------------------------------------------------------------------------

clear

%% 12/01/2022 -- CW at 1575.42 MHz
%
%  8-way splitter
%  HackRF One "RF gain" set to 14
%
%    HackRF One --> 8482A Power Sensor --> E4418B Power Meter
%
%     m =          1.09963398475264
%     b =          38.8025528924042
%{
    r = [; ...
        +01.0,   -35.262; ...
        +02.0,   -34.550; ...
        +03.0,   -33.749; ...
        +04.0,   -32.972; ...
        +05.0,   -32.157; ...
        +06.0,   -31.301; ...
        +07.0,   -30.390; ...
        +08.0,   -29.599; ...
        +09.0,   -28.656; ...
        +10.0,   -27.815; ...
        +11.0,   -26.927; ...
        +12.0,   -26.033; ...
        +13.0,   -25.151; ...
        +14.0,   -24.278; ...
        +15.0,   -23.350; ...
        +16.0,   -21.152; ...
        +17.0,   -19.980; ...
        +18.0,   -18.998; ...
        +19.0,   -18.145; ...
        +20.0,   -17.156; ...
        +21.0,   -16.270; ...
        +22.0,   -15.346; ...
        +23.0,   -14.367; ...
        +24.0,   -13.419; ...
        +25.0,   -12.576; ...
        +26.0,   -11.623; ...
        +27.0,   -10.687; ...
        +28.0,   -09.802; ...
        +29.0,   -08.868; ...
        +30.0,   -07.973; ...
        +31.0,   -07.086; ...
        +32.0,   -06.136; ...
        +33.0,   -05.261; ...
        +34.0,   -04.347; ...
        +35.0,   -03.447; ...
        +36.0,   -02.533; ...
        +37.0,   -01.649; ...
        +38.0,   -00.726; ...
        +39.0,   +00.184; ...
        +40.0,   +01.095; ...
        +41.0,   +01.997; ...
        +42.0,   +02.905; ...
        +43.0,   +03.826; ...
        +44.0,   +04.723; ...
        +45.0,   +05.622; ...
        +46.0,   +06.513; ...
        +47.0,   +07.388; ...
        ];
%}

%% 12/01/2022 -- CW at 1575.42 MHz
%
%  8-way splitter
%  HackRF One "RF gain" set to 0
%
%    HackRF One --> 8482A Power Sensor --> E4418B Power Meter
%
%     m =           1.0490291635255
%     b =          53.4437303383469
%{
    r = [; ...
        +01.0,   -39.854; ...
        +02.0,   -39.992; ...
        +03.0,   -39.830; ...
        +04.0,   -39.290; ...
        +05.0,   -40.071; ...
        +06.0,   -39.932; ...
        +07.0,   -41.085; ...
        +08.0,   -41.049; ...
        +09.0,   -41.422; ...
        +10.0,   -41.533; ...
        +11.0,   -43.888; ...
        +12.0,   -46.978; ...
        +13.0,   -58.587; ...
        +14.0,   -50.640; ...
        +15.0,   -42.771; ...
        +16.0,   -38.658; ...
        +17.0,   -36.257; ...
        +18.0,   -34.697; ...
        +19.0,   -33.519; ...
        +20.0,   -32.442; ...
        +21.0,   -31.260; ...
        +22.0,   -30.265; ...
        +23.0,   -29.033; ...
        +24.0,   -27.953; ...
        +25.0,   -27.054; ...
        +26.0,   -26.027; ...
        +27.0,   -25.028; ...
        +28.0,   -24.113; ...
        +29.0,   -23.164; ...
        +30.0,   -22.237; ...
        +31.0,   -21.132; ...
        +32.0,   -20.170; ...
        +33.0,   -19.287; ...
        +34.0,   -18.344; ...
        +35.0,   -17.445; ...
        +36.0,   -16.516; ...
        +37.0,   -15.627; ...
        +38.0,   -14.686; ...
        +39.0,   -13.766; ...
        +40.0,   -12.840; ...
        +41.0,   -11.921; ...
        +42.0,   -10.998; ...
        +43.0,   -10.059; ...
        +44.0,   -09.131; ...
        +45.0,   -08.208; ...
        +46.0,   -07.273; ...
        +47.0,   -06.346; ...
        ];
%}

%% --- No changes should be required below this line -----------------------

% Build column vectors
%   x == HackRF One output power [dBm]
%   y == Osmocom Sink IF gain [dB]
x = r(:,2);
y = r(:,1);

% Only curve-fit for IF gain >= 20
idx = (y >= 20);

% solve for linear fit coefficients
p = pinv([x(idx) ones(size(x(idx)))]) * y(idx);

% y = m*x + b
m = p(1) %#ok<NOPTS> 
b = p(2) %#ok<NOPTS> 

figure(1);
clf(1);
box('on');
grid('on');
hold('on');
plot(x,y,'b-','LineWidth',1);
plot(x(idx),y(idx),'bo','LineWidth',2,'MarkerSize',10);
plot(x(~idx),y(~idx),'kx','LineWidth',2,'MarkerSize',7);
xlim([-55 10]);
ylim([0 50]);
xlabel('HackRF Ouput Power [dBm]','FontSize',14);
ylabel('Osmocom Sink IF Gain [dB]','FontSize',14);

xx = -55:10;
yy = m*xx + b;

plot(xx,yy,'r','LineWidth',2);

%%
return
%-------------------------------------------------------------------------------