%%Note to users:
%%This will create a slightly different fit (and thus figure) than the R version
%%The R code uses #ns, the built in function to generate a natural cubic spline 
%%basis, which Matlab does not have.  While the spline toolbox function: cscvn 
%%fits a natural spline basis e.g. via:
%%>fnplt(cscvn([knots; lfp1(knots)']))
%%while end conditions are "natural", it fits using a cumulative chordal distance
%%representation instead.  We could also build a basis ourselves, although 
%%most methods will perform similarly well given appropriately placed knots

lfp = load('lfp-ryan.dat');

time = 1:1000;
lfp1 = lfp(1:1000);
lfp30 = lfp(29001:30000);

knots = [1, 200:200:1000];
splinefit1 = spline(knots, lfp1(knots), time);
splinefit30 = spline(knots, lfp30(knots), time);

subplot(2, 1, 1)
plot(lfp1, '-k', 'LineWidth', 2)
hold on;
plot(splinefit1, '-b', 'LineWidth', 2)

set(gca, 'Box', 'off', 'FontSize', 22, 'YLim', [-70, 65], ...
         'XTick', 0:200:1000, 'YTick', -60:30:60, 'TickDir', 'out')
xlabel('Time (ms)', 'FontSize', 22)
ylabel('lfp1', 'FontSize', 22)

subplot(2, 1, 2)
plot(lfp30, '-k', 'LineWidth', 2)
hold on;
plot(splinefit30, '-b', 'LineWidth', 2)

set(gca, 'Box', 'off', 'FontSize', 22, 'YLim', [-40, 140], ...
         'XTick', 0:200:1000, 'YTick', 0:50:100, 'TickDir', 'out')
xlabel('Time (ms)', 'FontSize', 22)
ylabel('lfp30', 'FontSize', 22)

set(gcf, 'Position', [200, 100, 1000, 800])