comparison aim-mat/modules/usermodule/pitchstrength/genpitchstrength2.m @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents
children
comparison
equal deleted inserted replaced
3:20ada0af3d7d 4:537f939baef0
1 % generating function for 'aim-mat'
2 %
3 % INPUT VALUES:
4 %
5 % RETURN VALUE:
6 %
7 %
8 % (c) 2003, University of Cambridge, Medical Research Council
9 % Christoph Lindner
10 % (c) 2011, University of Southampton
11 % Maintained by Stefan Bleeck (bleeck@gmail.com)
12 % download of current version is on the soundsoftware site:
13 % http://code.soundsoftware.ac.uk/projects/aimmat
14 % documentation and everything is on http://www.acousticscale.org
15
16
17 function ps_result=genpitchstrength(sai,options)
18
19 target_frequency=options.target_frequency;
20
21 if target_frequency > 0
22 look_for_target_frequency=1;
23 minimum_allowed_frequency=target_frequency/options.allowed_frequency_deviation;
24 maximum_allowed_frequency=target_frequency*options.allowed_frequency_deviation;
25 else
26 look_for_target_frequency=0;
27 end
28
29 if isfield(options,'resolved_harmonic_minimum')
30 resolved_harmonic_minimum=options.resolved_harmonic_minimum;
31 end
32
33
34 % find out about scaling:
35 maxval=-inf;
36 maxfreval=-inf;
37 maxsumval=-inf;
38
39 nr_frames=length(sai);
40 for ii=1:nr_frames
41 maxval=max([maxval getmaximumvalue(sai{ii})]);
42 maxsumval=max([maxsumval getscalesumme(sai{ii})]);
43 maxfreval=max([maxfreval getscalefrequency(sai{ii})]);
44 end
45
46
47 for frame_number=1:nr_frames
48 current_frame = sai{frame_number};
49 % current_frame = setallmaxvalue(current_frame, maxval);
50 frame_start_time(frame_number)=getcurrentframestarttime(current_frame);
51 tip(frame_number)=gettimeintervalprofile(current_frame,options);
52 end
53 frame_duration=frame_start_time(2)-frame_start_time(1);
54
55 % calculate the averaged tips
56 % the first frames are not average, since not long enough time has
57 % passed. After that, the relevant last frames are averaged
58 tip_time=options.tip_average_time;
59 % time for pitch to drop to 1/2 (6dB) --in seconds
60 % this means so much per frame
61 ps_decay_constant=1-log(2)/(tip_time/frame_duration);
62
63
64 % % and again to sum them up
65 % for frame_number=1:nr_frames
66 % thisav=mute(tip(frame_number)); % take a copy of the original one
67 % figure(1243234)
68 % clf
69 % hold on
70 % count=0;
71 % for recent_frames_nr=frame_number-nr_frames_to_average:frame_number
72 % if recent_frames_nr > 0
73 % thisav=thisav+tip(recent_frames_nr);
74 % count=count+1;
75 % plot(tip(recent_frames_nr),'g');
76 % end
77 % end
78 % thisav=thisav/count; % the average
79 % plot(thisav,'r');
80 % average_tip(frame_number)=thisav;
81 % end
82
83 % is it necessary to calculate all, or can we start right at the end?
84 % if options.return_only_last==1
85 % start_frame=length(sai);
86 % else % no we
87 start_frame=1;
88 % end
89
90 emptysig=mute(tip(1));
91 % mache ein Histogramm für jede Frequenz mit allem Maxima:
92 nr_points=getnrpoints(sai{1});
93 histo=zeros(1,nr_points);
94
95 waithand = waitbar(0,'Generating pitch strength');
96 for frame_number=start_frame:nr_frames
97 % for frame_number=1:10
98
99 waitbar(frame_number/nr_frames, waithand);
100
101 current_frame = sai{frame_number};
102 current_frame = setallmaxvalue(current_frame, maxval);
103 current_frame = setscalesumme(current_frame, maxsumval);
104 current_frame = setscalefrequency(current_frame, maxfreval);
105
106 interval_sig = tip(frame_number);
107 % interval_sig = average_tip(frame_number);
108 ps_result{frame_number}.frequency_sum = getfrequencysum(current_frame);
109 % Normalisation
110 interval_sig = interval_sig/getnrpoints( ps_result{frame_number}.frequency_sum);
111 ps_result{frame_number}.frequency_sum = ps_result{frame_number}.frequency_sum/getnrpoints(interval_sig)*options.scalefactor;
112
113 opts.ps_options=options;
114 % do all relevant pitch calculations for this frame
115 result=find_pitches(interval_sig,opts);
116
117 ps_result{frame_number}.interval_sum =emptysig;
118 ps_result{frame_number}.pitchstrength=0;
119
120
121 % sum all pitches for a histogram
122 peaks=result.peaks;
123 nr_peaks=length(peaks);
124 if nr_peaks>0
125 for current_peak=1:nr_peaks
126 peak_time=peaks{current_peak}.t;
127 peak_x=peaks{current_peak}.x;
128 peak_height=peaks{current_peak}.y;
129 int_x=round(peak_x);
130 histo(int_x)=histo(int_x)+peak_height;
131 end
132
133 shist=signal(histo,getsr(interval_sig));
134 shistl=lowpass(shist,400);
135 peaks=IPeakPicker(shistl);
136
137 % ignore the first peak on the left:
138 % speaks=sortstruct(peaks,'x');
139 % peaks{1}.y=0;
140
141 speaks=sortstruct(peaks,'y');
142
143 pitchstrength=speaks{1}.y;
144 final_dominant_frequency=1/speaks{1}.t;
145 %
146 % if mod(frame_number,10)==0
147 % figure(43242)
148 % if frame_number<30
149 % clf;
150 % hold on
151 % end
152 % plot(shist/50);
153 % % ax=axis;
154 % plot(shistl,'r'); hold on
155 % % axis(ax);
156 % fre=1/speaks{1}.t;
157 % plot(speaks{1}.x,speaks{1}.y,'o','MarkerFaceColor','g','MarkerEdgeColor','w','MarkerSize',10)
158 % text(speaks{1}.x,speaks{1}.y,sprintf('%2.0fHz %2.2f',final_dominant_frequency,pitchstrength))
159 % for i=2:length(speaks)/2
160 % fre=1/speaks{i}.t;
161 % plot(speaks{i}.x,speaks{i}.y,'o','MarkerFaceColor','m','MarkerEdgeColor','w','MarkerSize',5)
162 % text(speaks{i}.x,speaks{i}.y,sprintf('%2.0fHz %2.2f',fre,speaks{i}.y))
163 % end
164 % time=frame_number*5;
165 % text(speaks{1}.x+400,speaks{1}.y,sprintf('Time: %3.0fms',time));
166 % o=0;
167 % end
168
169 ps_result{frame_number}.peaks = speaks;
170 ps_result{frame_number}.interval_sum =shistl;
171
172 if look_for_target_frequency
173 count=0;
174 for ii=1:length(speaks) % construct all maxima
175 fre=1/speaks{ii}.t;
176 dist(ii)=abs(target_frequency-fre);
177 if fre> minimum_allowed_frequency && fre < maximum_allowed_frequency
178 count=count+1;
179 allowednumber(count)=ii;
180 allowedheights(count)=speaks{ii}.y;
181 end
182 end
183 % [minfredif,nearest_peak_number]=min(dist);
184 % fre=1/speaks{nearest_peak_number}.t;
185 % is the frequency allowed?
186 if count>0
187 % if fre< minimum_allowed_frequency || fre > maximum_allowed_frequency
188 [highest_ps,best_number]=max(allowedheights);
189 best_peak_number=allowednumber(best_number);
190
191 % gib das Verhältnis der peakstrength zu allen peaks
192 % zurück (ohne ihm selbst)
193 do_only_relationship=1;
194 if do_only_relationship
195 count=1;
196 for ii=1:length(speaks)
197 if ii~=best_peak_number
198 allps(count)=speaks{ii}.y;
199 count=count+1;
200 end
201 end
202 ps=speaks{best_peak_number}.y/mean(allps);
203 ps_result{frame_number}.pitchstrength=ps;
204 ps_result{frame_number}.dominant_frequency=1/speaks{best_peak_number}.t;
205 else
206 ps_result{frame_number}.pitchstrength=speaks{best_peak_number}.y;
207 ps_result{frame_number}.dominant_frequency=1/speaks{best_peak_number}.t;
208 end
209 else % no, nothing there
210 % ps_result{frame_number}.interval_sum = result.smoothed_signal;
211 ps_result{frame_number}.peaks = [];
212 ps_result{frame_number}.dominant_frequency=0;
213 ps_result{frame_number}.pitchstrength=0;
214 end % frequency allowance
215 else % free search was already done!
216 ps_result{frame_number}.dominant_frequency=final_dominant_frequency;
217 ps_result{frame_number}.pitchstrength=pitchstrength;
218 end % look for target frequency
219 else % no peaks found
220 ps_result{frame_number}.peaks = [];
221 ps_result{frame_number}.dominant_frequency=0;
222 ps_result{frame_number}.pitchstrength=0;
223 end
224
225 % dynamic decrease of all pitch strengthes
226 histo=histo.*ps_decay_constant;
227
228
229 % Peak Picker for FrequencyProfile
230 p.dyn_thresh = options.dynamic_threshold_FP;
231 p.smooth_sigma = options.smooth_sigma_FP;
232 ps_result{frame_number}.peaks_frequency_sum = FPeakPicker(ps_result{frame_number}.frequency_sum, p);
233 % ps_result{frame_number}.channel_center_fq = getcf(sai{frame_number});
234 ps_result{frame_number}.channel_center_fq = getcf(sai{frame_number});
235
236 end
237 close(waithand);
238
239