Skip to content

Commit e2d4fc0

Browse files
committed
Playing around with spikes
1 parent c6f3188 commit e2d4fc0

File tree

7 files changed

+354
-3
lines changed

7 files changed

+354
-3
lines changed

constants/johnsonCurrent.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function rms = johnsonCurrent(R,T,deltaF)
2+
% johnson noise in current as a function of temp T, bandwidth deltaF, R
3+
rms = sqrt(4*kb*T*deltaF/R);

constants/kb.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function c = kb()
2+
% Boltzmann's constant, 1.38 e-23 J/K
3+
c = 1.38e-23;

kathy-analysis/poissonSpikes.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function s = poissonSpikes(r,sr,n)
1+
function s = poissonSpikes(r,sr,n,varargin)
22
% s = poissonSpike(r,sr,n)
33
% generate poisson distributed spike train s
44
% given rate function r (Hz) and sample rate sr
@@ -7,11 +7,18 @@
77
if nargin <3,
88
n = 1;
99
end
10+
if nargin>4
11+
sd = varagin{1};
12+
else
13+
sd = [];
14+
end
1015

1116
r = r(:);
1217
T = length(r);
1318
test = repmat(r,1,n)/sr;
1419

1520
s = zeros(T,n);
16-
x = rand(T,n);
17-
s(find(x<test)) = 1;
21+
if isempty(sd), x = rand(T,n);
22+
else rng(sd), x = rand(T,n);
23+
end
24+
s(x<test) = 1;

plotting/plotMatrixRaster.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function ax = plotMatrixRaster(s,varargin)
2+
% s is a matrix of spikes in time, columns are trials
3+
4+
if nargin>1
5+
t = varargin{1};
6+
else
7+
t = 1:size(s,1);
8+
t = t(:);
9+
end
10+
11+
ax = gca;
12+
axes(ax);
13+
for c = 1:size(s,2)
14+
r = size(s,2)-c+1;
15+
sp_times = find(s(:,c));
16+
r = repmat(size(s,2)-c+1,size(sp_times));
17+
18+
linesH = line([t(sp_times)'; t(sp_times)'],[r' - 0.4; r' + 0.4]);
19+
set(linesH,'color','k');
20+
end
21+
ylabel(ax, 'Trial Number');
22+
xlabel(ax, 'Time (s)');
23+
set(ax,'ytick',1:c);
24+
xlim(ax,[0 t(end)]);
25+
ylim(ax,[0 c+1]);
26+
27+
set(ax,'tag','plotAxis'); %reset tag (it was getting lost - I don't know how)
28+

plotting/plotRaster.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function results = plotRaster(s,varargin)
2+
if nargin>1
3+
t = varargin{1};
4+
end
5+
6+
ax = gca;
7+
axes(ax);
8+
for c=
9+
linesH = line([sp_times'; sp_times'],[trial_numbers' - 0.4; trial_numbers' + 0.4]);
10+
set(linesH,'color','k');
11+
ylabel(ax, 'Trial Number');
12+
xlabel(ax, 'Time (s)');
13+
set(ax,'ytick',1:Ntrials);
14+
xlim(ax,[0 duration]);
15+
ylim(ax,[0 Ntrials+1]);
16+
17+
set(ax,'tag','plotAxis'); %reset tag (it was getting lost - I don't know how)
18+

sandbox/LNSandbox.m

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
%% Kathy's filters sandbox
2+
3+
%% Shape a filter
4+
N = 10000;
5+
samprate = 10000;
6+
nfft = 1024;
7+
8+
% make freq vector
9+
w = (0:nfft/2)';
10+
11+
% make a PS, reflect about 0
12+
sig = 20;
13+
fw = exp(-w.^2/sig^2);
14+
fw = [fw;flipud(fw(2:end-1))];
15+
16+
% plot the power spectrum
17+
plot([-flipud(w(2:end-1));w],fftshift(fw));
18+
19+
f = samprate/nfft*[0:nfft/2]; f = [f, fliplr(f(2:end-1))];
20+
21+
% fft back (try changing phases, check it out)
22+
c = real(ifft(sqrt(fw)));
23+
figure;
24+
plot(c);
25+
26+
%% Shape a filter in the time domain
27+
samprate = 10000;
28+
T = 1/samprate;
29+
N = 10000;
30+
t = (0:N-1)/samprate;
31+
32+
% filter: Hermite Sharpee, Doupe
33+
t_0 = .1;
34+
tau = .006;
35+
H0 = pi^(-1/4)*exp(-((t-t_0)/tau).^2/2);
36+
H1 = sqrt(2)*pi^(-1/4)*((t-t_0)/tau).* exp(-((t-t_0)/tau).^2/2);
37+
H2 = pi^(-1/4)*sqrt(2)*(2*((t-t_0)/tau).^2-1).*exp(-((t-t_0)/tau).^2/2);
38+
39+
nfft = 2^nextpow2(length(t));
40+
f = samprate/nfft*[0:nfft/2]; f = [f, fliplr(f(2:end-1))];
41+
42+
% plot(t,H0,t,H1,t,H2)
43+
fH0 = fft(H0,nfft);
44+
fH1 = fft(H1,nfft);
45+
fH2 = fft(H2,nfft);
46+
47+
impulse = zeros(length(t),1)/samprate;
48+
impulse(1) = samprate;
49+
50+
% make a butterworth
51+
52+
[B,A] = butter(2,100/samprate);
53+
b = filter(B,A,impulse);
54+
55+
% freqs(B,A)
56+
% figure(2)
57+
% freqz(B,A)
58+
% figure(2)
59+
% plot(t,b);
60+
61+
% %% make a bessel
62+
63+
% [B,A] = besself(5,.001/2/pi);
64+
% bes = filtfilt(B,A,impulse);
65+
% figure(1)
66+
% freqs(B,A)
67+
% figure(2)
68+
% plot(t,bes);
69+
70+
% make a chebyschev
71+
72+
c = H0;
73+
c = H1;
74+
c = H2;
75+
c = b;
76+
% c = bes;
77+
78+
fc = fft(c,nfft);
79+
80+
% look at magnitude
81+
82+
figure(1)
83+
subplot(4,1,1)
84+
loglog(f,abs(fc))
85+
title('mag')
86+
87+
% look at phase delay
88+
subplot(4,1,2)
89+
semilogx(f,unwrap(angle(fc)))
90+
title('phase')
91+
92+
% look at power
93+
subplot(4,1,3)
94+
loglog(f,real(fc.*conj(fc)))
95+
title('power')
96+
97+
% look at filter
98+
subplot(4,1,4)
99+
plot(t,c)
100+
title('power')
101+
102+
c = flipud(c);
103+
104+
%% Make a stimulus envelope
105+
alpha = 0;
106+
N = 100000;
107+
samprate = 10000;
108+
nfft = 2^nextpow2(N);
109+
tau = 50;
110+
111+
% choose a white noise stimulus
112+
stim = powernoise(alpha, N, 'randpower', 'normalize');
113+
stim = stim-mean(stim);
114+
115+
% filter, change in frequency domain
116+
fstim_pre = fft(stim,nfft);
117+
[pstim_pre, f_pre] = pwelch(stim,nfft/2,nfft/4,nfft,samprate);
118+
119+
f = samprate/nfft*[0:nfft/2]; f = [f, fliplr(f(2:end-1))]';
120+
fexp = exp(-f/(tau/2));
121+
122+
fstim_post = fstim_pre.*fexp;
123+
env = ifft(fstim_post);
124+
env = env(1:N);
125+
[pstim_post, f_post] = pwelch(env,nfft/2,nfft/4,nfft,samprate);
126+
127+
% low pass
128+
129+
% gaussian band pass
130+
131+
% high pass
132+
133+
% pink
134+
135+
figure(1)
136+
subplot(4,1,1);
137+
plot(stim)
138+
139+
subplot(4,1,2);
140+
loglog(f,fstim_pre.*conj(fstim_pre),'b'); hold on;
141+
loglog(f,fstim_post.*conj(fstim_post),'r');
142+
143+
subplot(4,1,3);
144+
loglog(f_pre,pstim_pre,'b-'); hold on
145+
loglog(f_post,pstim_post,'r-'); hold on
146+
147+
subplot(4,1,4);
148+
plot(env);
149+
150+
env_pre = env/std(env);
151+
152+
153+
% Make a stimulus
154+
155+
mu = 1;
156+
sig = 1;
157+
alpha = 0;
158+
159+
env = 10.^(mu + sig*env_pre);
160+
161+
% choose a white noise carrier
162+
stim = powernoise(alpha, N, 'randpower', 'normalize');
163+
stim = stim-mean(stim);
164+
165+
% choose a pure tone carrier
166+
% t = (1:N)'/samprate;
167+
% f = 1000;
168+
% stim = sin(f*t + 2*pi*(rand(1)-.5));
169+
% stim = stim-mean(stim);
170+
171+
stim = env.*stim;
172+
173+
figure(1)
174+
plot(env,'r'); hold on
175+
plot(stim); hold off
176+
177+
%sound(stim)
178+
179+
%% generate neural response
180+
181+
gain = 1e-3;
182+
183+
[r,tr] = predict(gain*c,stim,0,samprate);
184+
185+
% hold on;
186+
% plot(tr,r);
187+
% plot(ts,scale*stim/max(stim),'r');
188+
% axis([0 max(ts) 0 max(r)]);
189+
190+
% generate spikes
191+
s = poissonSpikes(r,samprate,1,0);
192+
193+
acausal_short = 20;
194+
causal_short = -100;
195+
c_sta = [c(end+causal_short*samprate/1000:end);c(1:acausal_short*samprate/1000)];
196+
norm_c_sta = c_sta/sqrt(c_sta'*c_sta);
197+
198+
figure(1), clf
199+
subplot(3,1,1);
200+
plot(stim);
201+
202+
% [S,F,T,P] = spectrogram(stim,256,250,256,samprate);
203+
%
204+
% subplot(3,1,2);
205+
% colormap(pmkmp(256,'CubicL'))
206+
% surf(T,F,10*log10(P),'edgecolor','none'); axis tight;
207+
% %surf(T,F,(P),'edgecolor','none'); axis tight;
208+
% % colorbar
209+
% view(0,90);
210+
% title('White Noise');
211+
% xlabel('Time (Seconds)'); ylabel('Hz');
212+
213+
subplot(3,1,3);
214+
plotMatrixRaster(s);
215+
216+
217+
%% Predict filter
218+
acausal = 20;
219+
causal = -(length(stim)/samprate*1000-20);
220+
221+
filt = zeros(-causal*samprate/1000+acausal*samprate/1000+1,size(s,2));
222+
dfilt = filt;
223+
for col = 1:size(s,2);
224+
[filt(:,col),t] = quickfftxcorr(s(:,col),stim,samprate,causal,acausal);
225+
226+
end
227+
228+
filt_bar = mean(filt,2);
229+
norm_filt_bar = filt_bar/sqrt(filt_bar'*filt_bar);
230+
231+
%% decorrelate with stimulus
232+
233+
% set up the filters
234+
235+
nfft = 2^nextpow2(length(norm_filt_bar));
236+
f = samprate/nfft*[0:nfft/2]; f = [f, fliplr(f(2:end-1))]';
237+
238+
f_filt = fft(norm_filt_bar,nfft);
239+
f_stim = fft(stim,nfft);
240+
241+
dffilt = f_filt./(f_stim.*conj(f_stim));
242+
dfilt = ifft(dffilt);
243+
244+
% smooth the filter exponentially at really high frequencies
245+
246+
tau = 1000;
247+
f_cut = 1.2e2;
248+
249+
f = samprate/nfft*[0:nfft/2]; f = [f, fliplr(f(2:end-1))]';
250+
fexp = ones(size(f));
251+
fexp(f>f_cut) = exp(-(f(f>f_cut)-f_cut)/(tau/2));
252+
253+
fexp_filt = fft(norm_filt_bar,nfft).*fexp;
254+
fexp_dfilt = dffilt.*fexp;
255+
exp_dfilt = ifft(fexp_dfilt);
256+
257+
norm_filt_bar = norm_filt_bar(end-length(norm_c_sta)+1:end);
258+
dfilt = dfilt(end-length(norm_c_sta)+1:end);
259+
exp_dfilt = exp_dfilt(end-length(norm_c_sta)+1:end);
260+
261+
figure();
262+
loglog(f,f_filt.*conj(f_filt)); hold on
263+
loglog(f,f_stim.*conj(f_stim),'k');
264+
loglog(f,dffilt.*conj(dffilt),'r');
265+
266+
267+
%% plot the filtered filters and stimuli
268+
figure(2), clf
269+
subplot(2,1,1);
270+
% plot(filt,'k'); hold on
271+
% plot(mean(filt,2),'r');
272+
273+
figure(2)
274+
subplot(2,1,2);
275+
plot(mean(norm_filt_bar,2),'r'); hold on
276+
plot(norm_c_sta,'g')
277+
plot(exp_dfilt,'b');
278+
279+
figure(3)
280+
subplot(1,1,1);
281+
loglog(f,f_filt.*conj(f_filt)); hold on
282+
loglog(f,f_stim.*conj(f_stim),'k');
283+
loglog(f,dffilt.*conj(dffilt),'r');
284+
loglog(f,fexp.*conj(fexp),'b--');hold on
285+
loglog(f,fexp_dfilt.*conj(fexp_dfilt),'b');
286+
287+
nfft2 = 2^nextpow2(length(norm_c_sta));
288+
f2 = samprate/nfft2*[0:nfft2/2]; f2 = [f2, fliplr(f2(2:end-1))]';
289+
290+
loglog(f2,fft(norm_c_sta,nfft2).*conj(fft(norm_c_sta,nfft2)),'g');
291+

sandbox/SoundSandbox.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,4 @@
179179
view(0,90);
180180

181181

182+

0 commit comments

Comments
 (0)