-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfigure3_s1_panel_b.m
74 lines (57 loc) · 2.72 KB
/
figure3_s1_panel_b.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
72
73
74
%% script shows intuition for super-resolution
clear
clc
rng(2); % to get similar behavior each time
sampleInterval = 50; % sample every 500 ms
t = [0:500];
B = (1-exp(-t/2)).*(exp(-t/10)/10 - t.*exp(-t/20)/20/20);
A = 1;
% How far forward and back to calculate filter, in ms
extentForwards = 150;
extentBackwards = 10;
%% Show how more samples is better
impulse = zeros(extentBackwards+1+extentForwards,1);
impulse(extentBackwards+1) = 1;
impulseResponse = filter(B,A,impulse);
%Run through the same number of samples to generate exactly the same noise
%as figure3
for tLength = [1e5 5e4 1.5e4]
tVec = 1:tLength;
stim = randn(size(tVec));
resp = filter(B,A,stim);
resp = resp + std(resp)*randn(size(tVec)); % this sets SNR = 1!
end
sampleIdxs = 1:sampleInterval:max(tVec);
isSampled = zeros(size(resp));
isSampled(sampleIdxs) = 1;
responseSampled = resp.*isSampled;
responseSampled = responseSampled - mean(responseSampled);
stim = stim - mean(stim);
isSampled = zeros(size(resp));
isSampled(sampleIdxs) = 1;
stimMatrixForInterp = toeplitz(stim',zeros(1,extentForwards+extentBackwards+1));
stimMatrixForInterp = stimMatrixForInterp(extentForwards+extentBackwards+1:end,:);
stimMatrixForSuper = toeplitz(stim',zeros(1,extentForwards+extentBackwards+1+sampleInterval*2));
stimMatrixForSuper = stimMatrixForSuper(extentForwards+extentBackwards+1+sampleInterval*2:end,:);
figure(1);
clf;
responseInterped = interp1(sampleIdxs,responseSampled(logical(isSampled)),tVec);
numSamples = sum(isSampled(extentForwards+1+sampleInterval:end-extentBackwards-sampleInterval));
normalXcorr = responseSampled(1,extentForwards+1+sampleInterval:end-extentBackwards-sampleInterval)*stimMatrixForSuper/numSamples;
sampFreq = 1/sampleInterval;
triangleFilter = [0:sampFreq:1, 1-sampFreq:-sampFreq:0];
triangleFilter = triangleFilter./sum(triangleFilter);
normalXcorrFiltered = conv(normalXcorr,triangleFilter,'valid');
numSamplesInterp = length(isSampled(extentForwards+1+sampleInterval:end-extentBackwards-sampleInterval));
interpedXcorr = responseInterped(1,extentForwards+1+sampleInterval:end-extentBackwards-sampleInterval)*stimMatrixForSuper/numSamplesInterp;
deltaFilter = zeros(size(triangleFilter));
deltaFilter(ceil(length(deltaFilter)/2)) = 1;
interpedXcorr = conv(interpedXcorr,deltaFilter,'valid');
hold on;
plot([-extentBackwards:extentForwards]*10,[impulseResponse,normalXcorr(sampleInterval+1:end-sampleInterval)'])
plot([-extentBackwards:extentForwards]*10,interpedXcorr','LineWidth',2);
plot([-extentBackwards:extentForwards]*10,normalXcorrFiltered');
hold off
title('Triangle Filtering VS Interpolation XCorr')
legend({'Ground Truth','No Filtering','Interpolated','Super Res Triangle Filtered'})
set(gca,'xlim',[-extentBackwards,extentForwards]*10);