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