comparison aim-mat/modules/usermodule/pitchstrength/FPeakPicker.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 %
2 % function out = PeakPicker(sig_in, params)
3 %
4 % Find the peaks of a signal
5 %
6 % INPUT VALUES:
7 % sig_in Input signal
8 % params.dyn_thresh dynamic threshold. Off if not used
9 % params.smooth_sigma sigma for smoothing
10 %
11 %
12 %
13 %
14 % RETURN VALUE:
15 % out is an array of a struct
16 % out.x x position of the Peak
17 % out.t according time value
18 % out.y y value of the peak
19 % out.left.[x,t,y] left Minumum
20 % out.right.[x,t,y] right Minumum
21 %
22 % (c) 2003, University of Cambridge, Medical Research Council
23 % Christoph Lindner
24 % (c) 2011, University of Southampton
25 % Maintained by Stefan Bleeck (bleeck@gmail.com)
26 % download of current version is on the soundsoftware site:
27 % http://code.soundsoftware.ac.uk/projects/aimmat
28 % documentation and everything is on http://www.acousticscale.org
29
30 function out = PeakPicker(sig_in, params)
31
32
33 % ----- Other Parameters -----
34 % Lowpass param for the higpassfilter
35 LP_sigma_for_HP_filter = getnrpoints(sig_in)/7;
36 LP_sigma_for_smooth = 3;
37 % min width of a peak: upper_threshold+lower_thresh
38 upper_thresh = 0.02*getnrpoints(sig_in);
39 lower_thresh = 0.03*getnrpoints(sig_in);
40
41 % x is index or position in vector
42 % y is value of the
43 % t is the time domain wich is assigned to the x dimension
44
45 % the original values befor filtering
46 orig_values = getdata(sig_in)';
47
48 if isfield(params,'smooth_sigma')
49 if (params.smooth_sigma~=0)
50 % smooth the curve to kill small side peaks
51 sig_in = smooth(sig_in, params.smooth_sigma);
52 end
53 end
54
55 values=getdata(sig_in)';
56
57 % ------------------- Find the local maxima ------------------------------
58 % find x positions of ALL local maxima, incl. zero!!
59 max_x = find((values >= [0 values(1:end-1)]) & (values > [values(2:end) 0]));
60 if isfield(params,'smooth_sigma')
61 if (params.smooth_sigma~=0)
62 % smoothing might have shifted the positions of maxima.
63 % Therefore the maximum is the highest of the neighbours in
64 % distance +- smooth_sigma
65 for i=1:length(max_x)
66 start = max_x(i)-params.smooth_sigma;
67 if start<1
68 start=1;
69 end
70 stop = max_x(i)+params.smooth_sigma;
71 if stop>length(orig_values)
72 stop=length(orig_values);
73 end
74 m = find(orig_values(start:stop) == max(orig_values(start:stop)));
75 m=m(1);
76 max_x(i)=start-1+m;
77 end
78 end
79 end
80 max_y = orig_values(max_x);
81 orig_max_y = orig_values(max_x);
82
83 % ------------------- Find the local minima -----------------------------
84 min_x = find((values < [inf values(1:end-1)]) & (values <= [values(2:end) inf]));
85 min_y = values(min_x);
86
87
88 % peakpos_x=zeros(1,length(max_x));
89 peakpos_x=[];
90 for i=1:length(max_x),
91 % only take the highest peak
92 my = [max_y==max(max_y)]; % find the highest peak
93 % peakpos_x(i) = max_x(my); % x pos of highest peak
94 peakpos_x = [peakpos_x max_x(my)];
95 max_y = max_y([max_y<max(max_y)]); % del max value in y domain
96 max_x = max_x([max_x ~= peakpos_x(end)]); % and in x domain
97 end
98 peakpos_y = orig_values(peakpos_x); % extract the y vector
99
100 % --------------- Dynamic Threshold ---------------------
101 % works relativ to the mean
102 if isfield(params,'dyn_thresh')
103 if (params.dyn_thresh~=0)
104 % dynamic thresholding
105 m = mean(orig_values);
106 dthr = params.dyn_thresh.*m;
107 peakpos_x = peakpos_x([peakpos_y>=dthr]);
108 peakpos_y = peakpos_y([peakpos_y>=dthr]);
109 end
110 end
111
112 maxima = cell(1, length(peakpos_x));
113 % find the left end right minima that belong to a maximum
114 for i=1:length(peakpos_x)
115 maxima{i}.x = peakpos_x(i);
116 maxima{i}.t = bin2time(sig_in, maxima{i}.x);
117 maxima{i}.y = orig_values(peakpos_x(i));
118
119 % find left and right minimum for this maximum
120 maxima{i}.left.x = max(min_x([min_x < maxima{i}.x]));
121 if isempty(maxima{i}.left.x)
122 maxima{i}.left.x = 1;
123 maxima{i}.left.t = 0;
124 maxima{i}.left.y = orig_values(maxima{i}.left.x);
125 else
126 maxima{i}.left.y = orig_values(maxima{i}.left.x);
127 maxima{i}.left.t = bin2time(sig_in, maxima{i}.left.x);
128 end
129 maxima{i}.right.x = min(min_x([min_x > maxima{i}.x]));
130 if isempty(maxima{i}.right.x)
131 maxima{i}.right.x = length(orig_values);
132 maxima{i}.right.t = 0;
133 maxima{i}.right.y = orig_values(maxima{i}.right.x);
134 else
135 maxima{i}.right.y = orig_values(maxima{i}.right.x);
136 maxima{i}.right.t = bin2time(sig_in, maxima{i}.right.x);
137 end
138 end
139
140
141 out = maxima;
142
143