comparison aim-mat/modules/usermodule/pitchstrength/genpitchstrength3.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 plot_switch=0;
20
21 target_frequency=options.target_frequency;
22
23 if target_frequency > 0
24 look_for_target_frequency=1;
25 minimum_allowed_frequency=target_frequency/options.allowed_frequency_deviation;
26 maximum_allowed_frequency=target_frequency*options.allowed_frequency_deviation;
27 else
28 look_for_target_frequency=0;
29 end
30
31
32 % find out about scaling:
33 maxval=-inf;
34 maxfreval=-inf;
35 maxsumval=-inf;
36
37 nr_frames=length(sai);
38 for ii=1:nr_frames
39 maxval=max([maxval getmaximumvalue(sai{ii})]);
40 maxsumval=max([maxsumval getscalesumme(sai{ii})]);
41 maxfreval=max([maxfreval getscalefrequency(sai{ii})]);
42 end
43
44
45 for frame_number=1:nr_frames
46 current_frame = sai{frame_number};
47 % current_frame = setallmaxvalue(current_frame, maxval);
48 frame_start_time(frame_number)=getcurrentframestarttime(current_frame);
49 tip(frame_number)=getsum(current_frame);
50 end
51 frame_duration=frame_start_time(2)-frame_start_time(1);
52
53 % calculate the averaged tips
54 % the first frames are not average, since not long enough time has
55 % passed. After that, the relevant last frames are averaged
56 tip_time=options.tip_average_time;
57 % time for pitch to drop to 1/2 (6dB) --in seconds
58 % this means so much per frame
59 ps_decay_constant=1-log(2)/(tip_time/frame_duration);
60
61
62 % % and again to sum them up
63 % for frame_number=1:nr_frames
64 % thisav=mute(tip(frame_number)); % take a copy of the original one
65 % figure(1243234)
66 % clf
67 % hold on
68 % count=0;
69 % for recent_frames_nr=frame_number-nr_frames_to_average:frame_number
70 % if recent_frames_nr > 0
71 % thisav=thisav+tip(recent_frames_nr);
72 % count=count+1;
73 % plot(tip(recent_frames_nr),'g');
74 % end
75 % end
76 % thisav=thisav/count; % the average
77 % plot(thisav,'r');
78 % average_tip(frame_number)=thisav;
79 % end
80
81 % is it necessary to calculate all, or can we start right at the end?
82 % if options.return_only_last==1
83 % start_frame=length(sai);
84 % else % no we
85 start_frame=1;
86 % end
87
88
89 tip_average=mute(tip(1)); % take a copy of the original one
90
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
98 waitbar(frame_number/nr_frames, waithand);
99
100 current_frame = sai{frame_number};
101 current_frame = setallmaxvalue(current_frame, maxval);
102 current_frame = setscalesumme(current_frame, maxsumval);
103 current_frame = setscalefrequency(current_frame, maxfreval);
104
105 interval_sig = tip(frame_number);
106 % interval_sig = average_tip(frame_number);
107 ps_result{frame_number}.frequency_sum = getfrequencysum(current_frame);
108 % Normalisation
109 interval_sig = interval_sig/getnrpoints( ps_result{frame_number}.frequency_sum);
110 ps_result{frame_number}.frequency_sum = ps_result{frame_number}.frequency_sum/getnrpoints(interval_sig)*options.scalefactor;
111
112 opts.ps_options=options;
113 % do all relevant pitch calculations for this frame
114
115
116 % add the profile to the average
117 tip_average=tip_average+tip(frame_number);
118 result=find_pitches(tip_average,opts);
119 tip_average=tip_average*ps_decay_constant;
120
121
122 % sum all pitches for a histogram
123 peaks=result.peaks;
124 nr_peaks=length(peaks);
125 if nr_peaks>0
126 % for current_peak=1:nr_peaks
127 % peak_time=peaks{current_peak}.t;
128 % peak_x=peaks{current_peak}.x;
129 % peak_height=peaks{current_peak}.y;
130 % int_x=round(peak_x);
131 % histo(int_x)=histo(int_x)+peak_height;
132 % end
133
134 % shist=signal(histo,getsr(interval_sig));
135 % shistl=lowpass(shist,400);
136 % peaks=IPeakPicker(shistl);
137 speaks=sortstruct(peaks,'y');
138
139 pitchstrength=speaks{1}.y/1000000;
140 final_dominant_frequency=1/speaks{1}.t;
141
142 % if mod(frame_number,20)==0
143 % % plot(shist);
144 % % ax=axis;
145 % figure(43242)
146 % plot(shistl,'r'); hold on
147 % % axis(ax);
148 % for i=1:1%length(speaks)
149 % fre=1/speaks{i}.t;
150 % plot(speaks{i}.x,speaks{i}.y,'o','MarkerFaceColor','g','MarkerEdgeColor','g')
151 % text(speaks{i}.x,speaks{i}.y,sprintf('%2.2fHz %2.2f',fre,speaks{i}.y))
152 % end
153 % time=frame_number*5;
154 % text(speaks{1}.x+200,speaks{1}.y,sprintf('Time: %3.0fms',time));
155 % o=0;
156 % end
157
158
159 ps_result{frame_number}.peaks = speaks;
160 ps_result{frame_number}.interval_sum = result.smoothed_signal;
161
162 if look_for_target_frequency
163 count=0;
164 for ii=1:length(speaks) % construct all maxima
165 fre=1/speaks{ii}.t;
166 dist(ii)=abs(target_frequency-fre);
167 if fre< minimum_allowed_frequency || fre > maximum_allowed_frequency
168 count=count+1;
169 allowednumber(count)=ii;
170 allowedheights(count)=speaks{ii}.y;
171 end
172 end
173 % [minfredif,nearest_peak_number]=min(dist);
174 % fre=1/speaks{nearest_peak_number}.t;
175 % is the frequency allowed?
176 if count>0
177 % if fre< minimum_allowed_frequency || fre > maximum_allowed_frequency
178 [highest_ps,best_number]=max(allowedheights);
179 best_peak_number=allowednumber(best_number);
180
181 ps_result{frame_number}.pitchstrength=speaks{best_peak_number}.y/1000;
182 ps_result{frame_number}.dominant_frequency=1/speaks{best_peak_number}.t;
183 else % no, nothing there
184 ps_result{frame_number}.interval_sum = result.smoothed_signal;
185 ps_result{frame_number}.peaks = [];
186 ps_result{frame_number}.dominant_frequency=0;
187 ps_result{frame_number}.pitchstrength=0;
188 end % frequency allowance
189 else % free search was already done!
190 ps_result{frame_number}.dominant_frequency=final_dominant_frequency;
191 ps_result{frame_number}.pitchstrength=pitchstrength;
192 end % look for target frequency
193 else % no peaks found
194 ps_result{frame_number}.interval_sum = result.smoothed_signal;
195 ps_result{frame_number}.peaks = [];
196 ps_result{frame_number}.dominant_frequency=0;
197 ps_result{frame_number}.pitchstrength=0;
198 end
199
200
201 % Peak Picker for FrequencyProfile
202 p.dyn_thresh = options.dynamic_threshold_FP;
203 p.smooth_sigma = options.smooth_sigma_FP;
204 ps_result{frame_number}.peaks_frequency_sum = FPeakPicker(ps_result{frame_number}.frequency_sum, p);
205 % ps_result{frame_number}.channel_center_fq = getcf(sai{frame_number});
206 ps_result{frame_number}.channel_center_fq = getcf(sai{frame_number});
207
208 end
209 close(waithand);
210
211
212
213