comparison userProgramsTim/track_formants_from_IPI_guy.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 [iih,IPIhisttime,IPIhistweight]=track_formants_from_IPI_guy(IFRAN_pattern, sfreq)
2 %
3 % tracks the formants according to an analysis proposed in Secker-Walker
4 % JASA 1990, section V.A
5 % Tim Jürgens, February 2011, code from Guy Brown included
6 %
7 % input: IFRAN_pattern: pattern of the auditory model (dependend on the number of modules used)
8 % first dimension: frequency channel,
9 % second dimension: time (samples)
10 % sfreq: sampling frequency
11 % output: iih: interpeak-interval histogram, matrix very similar
12 % the plot 5 in the Secker-Walker paper
13 %
14 %
15 %
16
17
18 time_axis = 0:1/sfreq:(size(IFRAN_pattern,2)-1)/sfreq;
19
20 %find how many samples of AN_pattern are 10ms and 3ms
21 %one_sample_is_a_time_of = time_axis(2);
22 [tmp, start_time_index] = min(abs(0-time_axis));
23 [tmp, stop10_time_index] = min(abs(0.01-time_axis));
24 number_of_samples10ms = stop10_time_index - start_time_index;
25
26 [tmp, stop3_time_index] = min(abs(0.003-time_axis));
27 number_of_samples3ms = stop3_time_index - start_time_index;
28 every_3ms = 1:number_of_samples3ms:size(IFRAN_pattern,2)-number_of_samples10ms;
29
30 hamm_window = hamming(11);
31 halfHamming = (length(hamm_window)-1)/2;
32
33 % window normalization
34
35 norm = conv(ones(1,floor(number_of_samples10ms/2)),hamm_window);
36 norm = norm(5+1:end-5)';
37 win_size = number_of_samples10ms;
38 half_win_size = floor(win_size/2);
39 hop_size = number_of_samples3ms;
40
41
42 %pre-allocation due to speed
43 %Acorr = zeros(size(IFRAN_pattern,1),size(every_3ms,2),number_of_samples10ms*2+1);
44 %RAcorr = zeros(size(IFRAN_pattern,1),size(every_3ms,2),number_of_samples10ms*2+1);
45 %SRAcorr = zeros(size(IFRAN_pattern,1),size(every_3ms,2),number_of_samples10ms*2+1-10);
46 IPIhisttime = zeros(size(IFRAN_pattern,1),size(every_3ms,2),3);
47 IPIhistweight = zeros(size(IFRAN_pattern,1),size(every_3ms,2),3); %maximum 3 peaks from the SRA
48 iih = zeros(half_win_size,size(every_3ms,2)+1);
49
50
51
52
53 for iCounter = 1:size(IFRAN_pattern,1) %each channel
54 fprintf('Channel No. %i\n',iCounter);
55 %time_counter = 1;
56 %for jCounter = every_3ms %every 3ms time segment
57
58
59
60 %% Guy's code
61 % enframe this signal
62
63 frames = enframe(IFRAN_pattern(iCounter,:),win_size,hop_size);
64
65 % compute the autocorrelation
66
67 acf = real(ifft(abs(fft(frames,[],2)).^2,[],2));
68 acf(acf<0)=0;
69 acf = sqrt(acf(:,1:half_win_size));
70
71 % smooth with hamming window and take the root
72
73 for frame=1:size(acf,1)
74
75 %%debug
76 %if iCounter == 130
77 % disp('here');
78 %end
79
80
81 sra = conv(acf(frame,:),hamm_window);
82 sra = sra(halfHamming+1:end-halfHamming)./norm';
83 df = [0 ; diff(sra')];
84 idx = find((df(1:end-1)>=0)&(df(2:end)<0));
85 % interpolate
86 a=df(idx);
87 b=df(idx+1);
88 idx = (idx-1+a./(a-b));
89 % get rid of a zero peak, if it exists
90 idx = idx(idx>1);
91 % peak values corresponding to these intervals
92 amp = interp1(1:length(sra),sra,idx,'linear');
93 % if required, remove peaks that lie below the mean sra
94 % note that we disregard the value at zero delay
95 %if (params.removePeaksBelowMean)
96 valid = find(amp>mean(sra(2:end)));
97 idx = idx(valid);
98 amp = amp(valid);
99 %end
100 % only use the first four peaks (three intervals)
101 idx = idx(1:min(4,length(idx)));
102 % find the intervals
103 interval = diff(idx);
104 % now histogram the intervals
105 if (~isempty(interval))
106 for k=1:length(interval),
107 iih(round(interval(k)),frame) = iih(round(interval(k)),frame)+amp(k);
108 IPIhisttime(iCounter,frame,k) = interval(k)/sfreq;
109 IPIhistweight(iCounter,frame,k) = amp(k);
110 end
111 end
112
113 end
114
115
116
117
118 %% end Guy's code
119
120
121 % %take the autocorrelation (ACF) of a 10ms-segment of each channel
122 % Acorr(iCounter,time_counter,:) = xcorr(IFRAN_pattern(iCounter,jCounter:number_of_samples10ms+jCounter),'biased'); %biased scales the ACF by the reciprocal of the length of the segment
123 % %root calculation
124 % RAcorr(iCounter,time_counter,:) = sqrt(abs(Acorr(iCounter,time_counter,:)));
125 %
126 % %smoothing using the 11-point hamming window
127 % for kCounter = 6:size(RAcorr(iCounter,time_counter,:),3)-5 %start with 6 and end with 5 samples
128 % %less the length of time_axis not to get in conflict with the length of
129 % %the hamm_window
130 % SRAcorr(iCounter,time_counter,kCounter-5) = ...
131 % squeeze(RAcorr(iCounter,time_counter,(kCounter-5):(kCounter+5)))'*hamm_window./sum(hamm_window);
132 % end
133 %
134 % %mean value of actual SRA
135 % SRA_mean = mean(SRAcorr(iCounter,time_counter,:));
136 %
137 % %find signed zero-crossings of the first derivative (=difference)
138 % z_crossings_indices = find(diff(sign(diff(squeeze(SRAcorr(iCounter,time_counter,:))))) < 0)+1; %+1 is necessary, because diff shortens vector by 1
139 % middle_index = ceil(size(SRAcorr(iCounter,time_counter,:),3)/2);
140 %
141 % validCounter = 1;
142 % valid_z_crossings_indices = [];
143 % %find valid zero-crossings (peak higher than meanvalue and within first 5 ms of SRA)
144 % for lCounter = 1:length(z_crossings_indices)
145 % if (SRAcorr(iCounter,time_counter,z_crossings_indices(lCounter)) > SRA_mean) && ...
146 % (abs(z_crossings_indices(lCounter)-middle_index) < round(number_of_samples10ms/2));
147 % valid_z_crossings_indices(validCounter) = z_crossings_indices(lCounter);
148 % validCounter = validCounter+1;
149 % end
150 % end
151 %
152 % %find main peak in the ACF
153 % [tmp,index_of_z_crossings_main_index] = min(abs(middle_index-valid_z_crossings_indices));
154 % if ~tmp == 0
155 % disp('middle peak not appropriately found');
156 % end
157 %
158 % %%% for debugging
159 % % if iCounter == 130
160 % % disp('here');
161 % % figure, plot(squeeze(SRAcorr(iCounter,time_counter,:)));
162 % % hold on, plot([1 length(squeeze(SRAcorr(iCounter,time_counter,:)))],[SRA_mean SRA_mean],'r-');
163 % % end
164 % %%%
165 %
166 % %generate IPI-histogram: take the first 3 intervals of SRAcorr
167 % %(positive delay) in the first 5 ms
168 % histcounter = 1;
169 % for lCounter = index_of_z_crossings_main_index+1:min([length(valid_z_crossings_indices(index_of_z_crossings_main_index+1:end)) 3])+index_of_z_crossings_main_index
170 % sampledifference = abs(valid_z_crossings_indices(lCounter)-valid_z_crossings_indices(lCounter-1));
171 % %the difference between two adjacent peaks in the SRA is taken
172 % %as IPI estimate
173 % IPIhisttime(iCounter,time_counter,histcounter) = sampledifference*one_sample_is_a_time_of;
174 % %the amplitude of the SRA at the start of the SRA interval is
175 % %taken as the IPIweight
176 % IPIhistweight(iCounter,time_counter,histcounter) = SRAcorr(iCounter,time_counter,valid_z_crossings_indices(lCounter-1));
177 % histcounter = histcounter + 1;
178 % end
179
180 %time_counter = time_counter+1;
181 end
182