comparison testPrograms/testACF.m @ 38:c2204b18f4a2 tip

End nov big change
author Ray Meddis <rmeddis@essex.ac.uk>
date Mon, 28 Nov 2011 13:34:28 +0000
parents
children
comparison
equal deleted inserted replaced
37:771a643d5c29 38:c2204b18f4a2
1 % function [P dt lags SACF]= testACF
2 % testACF is a *script* to demonstrate the smoothed ACF of
3 % Balaguer-Ballestera, E. Denham, S.L. and Meddis, R. (2008).
4 %
5 % Convert this to a *function* by uncommenting the first line
6 % The function returns the P matrix plotted in Figure 96.
7 % If a function is used, the following outputs are returned:
8 % P: smoothed SACF (lags x time matrix)
9 % dt: time interval between successive columns of P
10 % lags: lags used in computing P
11 % SACF: unsmoothed SACFs
12 %
13 % A range of options are supplied in the early part of the program
14 %
15 % #1
16 % Identify the model parameter file (in 'MAPparamsName')
17 %
18 % #2
19 % Identify the kind of model required (in 'AN_spikesOrProbability')
20 % 'probability' is recommended for ACF work
21 %
22 % #3
23 % Choose between a harmonic complex or file input
24 % by commenting out unwanted code
25 %
26 % #4
27 % Set the signal rms level (in leveldBSPL)
28 %
29 % #5
30 % Identify the model channel BFs in the vector 'BFlist'.
31 %
32 % #6
33 % Last minute changes to the model parameters can be made using
34 % the cell array of strings 'paramChanges'.
35 % This is used here to control the details of the ACF computations
36 % Read the notes in this section for more information
37 %
38 % displays:
39 % Figure 97 shows the AN response to the stimulus. this is a channel x time
40 % display. The z-axis (and colour) is the AN fiber firing rate
41 %
42 % Figure 96 shows the P-matrix, the smoothed SACF.
43 %
44 % Figure 89 shows a summary of the evolution of the unsmoothed SACF
45 % over time. If you wish to take a snapshot of the P-matrix at a
46 % particular time, this figure can help identify when to take it.
47 % The index on the y-axis, identifies the required row numbers
48 % of the P or SACF matrix, e.g. P(:,2000)
49 %
50 % On request, (filteredSACFParams.plotACFs=1) Figure 89 shows the channel
51 % by channel ACFs at intervals during the computation as a movie.
52 % The number of ACF displays is controlled by 'plotACFsInterval'
53 % and the movie can be slowed or speeded up using 'plotMoviePauses'
54 % (see paramChanges section below).
55
56 % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57 % This global will find results from MAP1_14
58 global savedInputSignal ANprobRateOutput ANoutput dt dtSpikes savedBFlist
59 % This global,from model parameter file
60 global filteredSACFParams
61
62 dbstop if error
63 restorePath=path;
64 addpath (['..' filesep 'MAP'], ['..' filesep 'wavFileStore'], ...
65 ['..' filesep 'utilities'])
66
67 % User sets up requirements
68 %% #1 parameter file name
69 MAPparamsName='Normal'; % recommended
70
71
72 %% #2 probability (fast) or spikes (slow) representation: select one
73 % AN_spikesOrProbability='spikes';
74 % or
75 AN_spikesOrProbability='probability'; % recommended
76
77 %% #3 A. harmonic sequence or B. speech file input
78 % Comment out unwanted code
79 % A. harmonic tone (Hz) - useful to demonstrate a broadband sound
80 sampleRate= 44100; % recommended 44100
81 signalType= 'tones';
82 duration=0.100; % seconds
83 beginSilence=0.020;
84 endSilence=0.020;
85 rampDuration=.005; % raised cosine ramp (seconds)
86
87 % toneFrequency is a vector of component frequencies
88 F0=120;
89 toneFrequency= [3*F0 4*F0 5*F0];
90
91 % or
92 % B. file input
93 signalType= 'file';
94 fileName='Oh No';
95 fileName='1z67931a_44kHz';
96
97
98 %% #4 rms level
99 leveldBSPL= 60; % dB SPL (80 for Lieberman)
100
101 %% #5 number of channels in the model
102 % 21-channel model (log spacing of BFs)
103 numChannels=21;
104 lowestBF=150; highestBF= 6000;
105 BFlist=round(logspace(log10(lowestBF), log10(highestBF), numChannels));
106
107 %% #6 change model parameters
108 % Parameter changes can be used to change one or more model parameters
109 % *after* the MAPparams file has been read (see manual)
110
111 % Take control of ACF parameters
112 % The filteredACF parameters are set in the MAPparamsNormal file
113 % However, it is convenient to change them here leving the file intacta
114 minPitch= 80; maxPitch= 500; numPitches=50;
115 minPitch= 200; maxPitch= 4000; numPitches=40;
116 maxLag=1/minPitch; minLag=1/maxPitch;
117 lags= linspace(minLag, maxLag, numPitches);
118
119 paramChanges={...
120 'filteredSACFParams.lags=lags; % autocorrelation lags vector;',...
121 'filteredSACFParams.acfTau= 2; % (Wiegrebe) time constant ACF;',...
122 'filteredSACFParams.lambda= 0.12; % slower filter to smooth ACF;',...
123 'filteredSACFParams.plotACFs=1; % plot ACFs while computing;',...
124 'filteredSACFParams.plotACFsInterval=0.01;',...
125 'filteredSACFParams.plotMoviePauses=.1; ',...
126 'filteredSACFParams.usePressnitzer=0; % attenuates ACF at long lags;',...
127 'filteredSACFParams.lagsProcedure= ''useAllLags'';',...
128 };
129
130 % Notes:
131 % acfTau: time constant of unsmoothed ACF
132 % lambda: time constant of smoothed ACFS
133 % plotACFs: plot ACFs during computation (0 to switch off, for speed)
134 % plotACFsInterval: sampling interval for plots
135 % plotMoviePauses: pause duration between frames to allow viewing
136 % usePressnitzer: gives low weights to long lags
137 % lagsProcedure: used to fiddle with output (ignore)
138
139 %% delare 'showMap' options to control graphical output
140 % see UTIL_showMAP for more options
141 showMapOptions=[];
142 % showMapOptions.showModelOutput=0; % plot of all stages
143 showMapOptions.surfAN=1; % surface plot of HSR response
144 showMapOptions.PSTHbinwidth=0.001; % smoothing for PSTH
145
146 if exist('fileName','var')
147 % needed for labeling plot
148 showMapOptions.fileName=fileName;
149 end
150
151 %% Generate stimuli
152 switch signalType
153 case 'tones'
154 % Create tone stimulus
155 dt=1/sampleRate; % seconds
156 time=dt: dt: duration;
157 inputSignal=sum(sin(2*pi*toneFrequency'*time), 1);
158 amp=10^(leveldBSPL/20)*28e-6; % converts to Pascals (peak)
159 inputSignal=amp*inputSignal;
160 % apply ramps
161 % catch rampTime error
162 if rampDuration>0.5*duration, rampDuration=duration/2; end
163 rampTime=dt:dt:rampDuration;
164 ramp=[0.5*(1+cos(2*pi*rampTime/(2*rampDuration)+pi)) ...
165 ones(1,length(time)-length(rampTime))];
166 inputSignal=inputSignal.*ramp;
167 ramp=fliplr(ramp);
168 inputSignal=inputSignal.*ramp;
169 % add silence
170 intialSilence= zeros(1,round(beginSilence/dt));
171 finalSilence= zeros(1,round(endSilence/dt));
172 inputSignal= [intialSilence inputSignal finalSilence];
173
174 case 'file'
175 %% file input simple or mixed
176 [inputSignal sampleRate]=wavread(fileName);
177 dt=1/sampleRate;
178 inputSignal=inputSignal(:,1);
179 targetRMS=20e-6*10^(leveldBSPL/20);
180 rms=(mean(inputSignal.^2))^0.5;
181 amp=targetRMS/rms;
182 inputSignal=inputSignal*amp;
183 end
184
185 wavplay(inputSignal, sampleRate)
186
187 %% run the model
188
189 fprintf('\n')
190 disp(['Signal duration= ' num2str(length(inputSignal)/sampleRate)])
191 disp([num2str(numChannels) ' channel model: ' AN_spikesOrProbability])
192 disp('Computing ...')
193
194 MAP1_14(inputSignal, sampleRate, BFlist, ...
195 MAPparamsName, AN_spikesOrProbability, paramChanges);
196
197
198 %% The model run is now complete. Now display the results
199 % display the AN response
200 UTIL_showMAP(showMapOptions)
201
202 % compute ACF
203 switch AN_spikesOrProbability
204 case 'probability'
205 % use only HSR fibers
206 inputToACF=ANprobRateOutput(end-length(savedBFlist)+1:end,:);
207 otherwise
208 inputToACF=ANoutput;
209 dt=dtSpikes;
210 end
211
212 disp ('computing ACF...')
213
214 % read paramChanges to get new filteredSACFParams
215 for i=1:length(paramChanges)
216 eval(paramChanges{i});
217 end
218
219 [P BFlist SACF]= filteredSACF(inputToACF, dt, savedBFlist, filteredSACFParams);
220 disp(' ACF done.')
221
222 %% plot original waveform on summary/smoothed ACF plot
223 figure(96), clf
224 subplot(3,1,3)
225 t=dt*(1:length(savedInputSignal));
226 plot(t,savedInputSignal, 'k')
227 xlim([0 t(end)])
228 title(['stimulus: ' num2str(leveldBSPL, '%4.0f') ' dB SPL']);
229
230 % plot SACF
231 figure(96)
232 subplot(2,1,1)
233 imagesc(P.^2)
234 colormap bone
235 ylabel('periodicities (Hz)'), xlabel('time (s)')
236 title('smoothed SACF. (periodicity x time)')
237 % y-axis specifies pitches (1/lags)
238 % Force MATLAB to show the lowest pitch
239 postedYvalues=[1 get(gca,'ytick')]; set(gca,'ytick',postedYvalues)
240 pitches=1./filteredSACFParams.lags;
241 set(gca,'ytickLabel', round(pitches(postedYvalues)))
242 % x-axis is time at which P is samples
243 [nCH nTimes]=size(P);
244 t=dt:dt:dt*nTimes;
245 tt=get(gca,'xtick');
246 set(gca,'xtickLabel', round(100*t(tt))/100)
247
248 %% On a new figure show a cascade of SACFs
249 figure(89), clf
250 % select 100 samples;
251 [r c]=size(SACF);
252 step=round(c/100);
253 idx=step:step:c;
254
255 UTIL_cascadePlot(SACF(:,idx)', 1./pitches)
256
257 xlabel('lag (s)'), ylabel('time pointer -->')
258 title(' SACF summary over time')
259 yValues=get(gca,'yTick');
260 set(gca,'yTickLabel', num2str(yValues'*100))
261
262 path(restorePath)
263