To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at git://github.com/rmeddis/MAP.git .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Revision:

root / userProgramsTim / track_formants_from_IPI_guy_olde.m @ 38:c2204b18f4a2

History | View | Annotate | Download (7.44 KB)

1 38:c2204b18f4a2 rmeddis
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.010-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));
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
        %include the zeroth peak
92
        %idx = [1 idx']';
93
        % peak values corresponding to these intervals
94
        amp = interp1(1:length(sra),sra,idx,'linear');
95
        % if required, remove peaks that lie below the mean sra
96
        % note that we disregard the value at zero delay
97
        %if (params.removePeaksBelowMean)
98
        valid = find(amp>1.2*mean(sra(1:end)));
99
        idx = idx(valid);
100
        amp = amp(valid);
101
        %end
102
        % only use the first four peaks (three intervals)
103
        idx = idx(1:min(4,length(idx)));
104
        % find the intervals
105
        interval = diff(idx);
106
        % now histogram the intervals
107
        if (~isempty(interval))
108
            for k=1:length(interval),
109
                iih(round(interval(k)),frame) = iih(round(interval(k)),frame)+amp(k);
110
                IPIhisttime(iCounter,frame,k) = interval(k)/sfreq;
111
                IPIhistweight(iCounter,frame,k) = amp(k);
112
            end
113
        end
114
115
    end
116
117
118
119
120
    %% end Guy's code
121
122
123
    %         %take the autocorrelation (ACF) of a 10ms-segment of each channel
124
    %         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
125
    %         %root calculation
126
    %         RAcorr(iCounter,time_counter,:) = sqrt(abs(Acorr(iCounter,time_counter,:)));
127
    %
128
    %         %smoothing using the 11-point hamming window
129
    %         for kCounter = 6:size(RAcorr(iCounter,time_counter,:),3)-5 %start with 6 and end with 5 samples
130
    %             %less the length of time_axis not to get in conflict with the length of
131
    %             %the hamm_window
132
    %             SRAcorr(iCounter,time_counter,kCounter-5) = ...
133
    %                 squeeze(RAcorr(iCounter,time_counter,(kCounter-5):(kCounter+5)))'*hamm_window./sum(hamm_window);
134
    %         end
135
    %
136
    %         %mean value of actual SRA
137
    %         SRA_mean = mean(SRAcorr(iCounter,time_counter,:));
138
    %
139
    %         %find signed zero-crossings of the first derivative (=difference)
140
    %         z_crossings_indices = find(diff(sign(diff(squeeze(SRAcorr(iCounter,time_counter,:))))) < 0)+1; %+1 is necessary, because diff shortens vector by 1
141
    %         middle_index = ceil(size(SRAcorr(iCounter,time_counter,:),3)/2);
142
    %
143
    %         validCounter = 1;
144
    %         valid_z_crossings_indices = [];
145
    %         %find valid zero-crossings (peak higher than meanvalue and within first 5 ms of SRA)
146
    %         for lCounter = 1:length(z_crossings_indices)
147
    %             if (SRAcorr(iCounter,time_counter,z_crossings_indices(lCounter)) > SRA_mean) && ...
148
    %                     (abs(z_crossings_indices(lCounter)-middle_index) < round(number_of_samples10ms/2));
149
    %                 valid_z_crossings_indices(validCounter) = z_crossings_indices(lCounter);
150
    %                 validCounter = validCounter+1;
151
    %             end
152
    %         end
153
    %
154
    %         %find main peak in the ACF
155
    %         [tmp,index_of_z_crossings_main_index] = min(abs(middle_index-valid_z_crossings_indices));
156
    %         if ~tmp == 0
157
    %             disp('middle peak not appropriately found');
158
    %         end
159
    %
160
    %         %%% for debugging
161
    % %         if iCounter == 130
162
    % %             disp('here');
163
    % %             figure, plot(squeeze(SRAcorr(iCounter,time_counter,:)));
164
    % %             hold on, plot([1 length(squeeze(SRAcorr(iCounter,time_counter,:)))],[SRA_mean SRA_mean],'r-');
165
    % %         end
166
    %         %%%
167
    %
168
    %         %generate IPI-histogram: take the first 3 intervals of SRAcorr
169
    %         %(positive delay) in the first 5 ms
170
    %         histcounter = 1;
171
    %         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
172
    %             sampledifference = abs(valid_z_crossings_indices(lCounter)-valid_z_crossings_indices(lCounter-1));
173
    %             %the difference between two adjacent peaks in the SRA is taken
174
    %             %as IPI estimate
175
    %             IPIhisttime(iCounter,time_counter,histcounter) = sampledifference*one_sample_is_a_time_of;
176
    %             %the amplitude of the SRA at the start of the SRA interval is
177
    %             %taken as the IPIweight
178
    %             IPIhistweight(iCounter,time_counter,histcounter) = SRAcorr(iCounter,time_counter,valid_z_crossings_indices(lCounter-1));
179
    %             histcounter = histcounter + 1;
180
    %         end
181
182
    %time_counter = time_counter+1;
183
end