-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyfitcode.m
71 lines (61 loc) · 2.03 KB
/
polyfitcode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
%Read in data
data = textread('ctrl myl 1.2.4.txt');
Time = data(:,1);
Voltage = data(:,2);
%Figure out length & number of traces
prompt = 'How many data points are in one trace? ';
tracelength = input(prompt); %length of one trace
numtraces = length(data)/tracelength; %number of traces
%Bring baseline of data plot to zero
subplot(1, 2, 1);
plot(data(:,1), data(:,2), 'b-');
grid on;
baseLineIndexes = Time > 0 & Voltage < 3 & Voltage > -3;
% Fit those to a line
xBaseLine = Time(baseLineIndexes);
yBaseLine = Voltage(baseLineIndexes);
% Overlay them in red.
hold on;
plot(xBaseLine, yBaseLine, 'r.', 'LineWidth', 2);
legend('Signal', 'Base Line');
% Set up figure properties:
% Enlarge figure to full screen.
%set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
%set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
%set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Fit a line to the baseline elements.
coefficients = polyfit(xBaseLine, yBaseLine, 1);
% Extrapolate everywhere
xFit = linspace(min(Time), max(Time), length(Time));
yFit = polyval(coefficients, xFit);
% Subtract the baseline from TanY
correctedTanY = Voltage - yFit';
% Clamp to 0
%correctedTanY = max(correctedTanY, 0);
% Plot baseline corrected
subplot(1, 2, 2);
plot(Time, correctedTanY, 'b-');
grid on;
legend('Corrected Signal');
Bdata = horzcat(Time, correctedTanY);
%Convert data to matrix of all the traces
tlmatrix = tracelength*ones(numtraces,1); %Creates a numtraces x 1 matrix of tracelength
traces = mat2cell(Bdata, tlmatrix, 2); %Creates a numtraces x 1 with a tracelength x 2 matrix (each trace) within each cell
%%%
new = [];
meantraces = [];
for j = 1:tracelength
for i=1:numtraces
y = traces{i,1} (j,:);
new = [new;y];
end
meantrace = mean(new);
new = [];
meantraces = [meantraces;meantrace];
end
figure(2)
plot(meantraces(:,1), meantraces(:,2), 'r-','LineWidth', 2)
hold on
plot (Time, Voltage, 'b-', 'LineWidth', 0.01)