42 #include "EST_cutils.h"
43 #include "EST_simplestats.h"
45 #include "EST_Track.h"
46 #include "EST_TrackFile.h"
47 #include "EST_Option.h"
48 #include "EST_track_aux.h"
49 #include "EST_error.h"
60 static
int sorttest(const
void *a, const
void *b)
62 float *c = (
float *)a;
63 float *d = (
float *)b;
64 float res = (*c - *d);
67 return (res < 0.0) ? -1 : 1;
72 if (stype ==
"median")
73 time_med_smooth(c, x);
75 time_mean_smooth(c, x);
78 void time_med_smooth(
EST_Track &c,
float x)
82 cerr <<
"Error: Time smoothing can only operate on fixed contours\n";
88 cerr <<
"Error in smoothing: time spacing problem\n";
91 int n = (int)(x / c.
shift());
93 simple_med_smooth(c, n, i);
96 void time_mean_smooth(
EST_Track &c,
float x)
100 int n = (int)(x / c.
shift());
103 simple_mean_smooth(c, n, j);
106 void simple_med_smooth(
EST_Track &c,
int n,
int channel)
122 float *m =
new float[n];
127 for (i = 0; i < h; ++i)
130 for (j = 0; j < k; ++j)
131 m[j] = c.
a(j, channel);
132 qsort(m, k,
sizeof(
float), sorttest);
139 for (j = 0; j < n; ++j)
140 m[j] = c.
a(i - h + j, channel);
142 qsort(m, n,
sizeof(
float), sorttest);
149 for (j = 0; j < k; ++j)
150 m[j] = c.
a(i - (k/2) + j, channel);
151 qsort(m, k,
sizeof(
float), sorttest);
156 c.
a(i,channel) = a[i];
162 void simple_mean_smooth(
EST_Track &c,
int n,
int channel)
169 for (i = 0; i < h; ++i)
173 for (j = 0; j < k; ++j)
174 sum += c.
a(j, channel);
175 a[i] = sum /(float) k;
183 for (j = 0; j < k; ++j)
184 sum += c.
a(i - h + j, channel);
185 a[i] = sum /(float) k;
192 for (j = 0; j < k; ++j)
193 sum += c.
a(i - (k/2) + j, channel);
194 a[i] = sum /(float) k;
198 c.
a(i,channel) = a[i];
208 tr.
a(i, j) = fabs(tr.
a(i, j));
215 meansd(tr, mean, sd);
216 normalise(tr, mean, sd, -1.0, 1.0);
221 float upper,
float lower)
223 for (
EST_Litem *p = trlist.head(); p; p = p->next())
224 normalise(trlist(p), mean, sd, upper, lower);
230 float upper,
float lower)
233 normalise(tr, mean(i), sd(i), i, upper, lower);
236 void normalise(
EST_Track &tr,
float mean,
float sd,
int channel,
237 float upper,
float lower)
245 tr.
a(i, channel) = ((((tr.
a(i, channel) - mean) / (4 *sd)) + 0.5)
246 * (upper -lower)) + lower;
266 dist = c.
t(i + 1) - c.
t(i);
269 : (c.
a(i + 1) - c.
a(i)) / dist;
270 diff.
t(i) = c.
t(i) + (dist / 2.0);
287 " channel EST_Track with " << b.
num_channels() <<
" channel EST_Track\n";
291 for (i = 0; i < size; ++i)
293 diff.a(i, j) = a.
a(i, j) - b.
a(i, j);
305 for (i = 0; i < size; ++i)
306 diff.
a(i, channel_a) = a.
a(i, channel_a) - b.
a(i, channel_b);
318 cerr <<
"Error: Couldn't find field named " << fname <<
325 cerr <<
"Error: Couldn't find field named " << fname <<
326 " in second Track\n";
333 return difference(a, b, ch_a, ch_b);
337 float mean(
const EST_Track &tr,
int channel )
340 EST_error(
"Tried to access channel %d of %d channel track",
347 for( i=0, n=0; i<tr_num_frames; ++i )
353 return mean/(float)n;
360 m.
resize( tr_num_channels, 0 );
362 for(
unsigned int i=0; i<tr_num_channels; ++i )
370 void meansd(
EST_Track &tr,
float &m,
float &sd,
int channel)
374 m = mean( tr, channel );
378 for( i=0, n=0; i<tr_num_frames; ++i)
380 var += pow(tr.
a_no_check(i, channel) - m, float(2.0));
385 var /= (float) (n-1);
402 for (i = 0; i < size; ++i)
404 sum += pow((a.
a(i, channel) - b.
a(i, channel)),
float(2.0));
406 sum = sqrt(sum / size);
415 for (i = 0; i < size; ++i)
419 sum += fabs(a.
a(i, channel) - b.
a(i, channel));
432 for (i = 0; i < size; ++i)
436 predict = b.
a(i, channelb);
437 real = a.
a(i, channela);
440 error = predict-real;
443 xx += predict*predict;
455 cout <<
"RMSE " << sqrt(se.
mean()) <<
" Correlation is " << cor
456 <<
" Mean (abs) Error " << e.
mean() <<
" (" << e.
stddev() <<
")"
469 meansd(a, m[i], sd[i], i);
472 void meansd(
EST_TrackList &tl,
float &mean,
float &sd,
int channel)
481 for (p = tl.head(); p; p = p->next())
482 for (i = 0; i < tl(p).num_frames(); ++i)
484 if (!tl(p).track_break(i))
486 mean += tl(p).a(i, channel);
493 for (p = tl.head(); p; p = p->next())
494 for (i = 0; i < tl(p).num_frames(); ++i)
495 if (!tl(p).track_break(i))
496 var += pow(tl(p).a(i, channel) - mean, float(2.0));
509 for (i = 0; i < tl.
first().num_channels(); ++i)
510 meansd(tl, m[i], sd[i], i);
522 " channel EST_Track with " << b.
num_channels() <<
" channel EST_Track\n";
527 e[i] = rms_error(a, b, i);
541 " channel EST_Track with " << b.
num_channels() <<
" channel EST_Track\n";
546 e[i] = abs_error(a, b, i);
560 " channel EST_Track with " << b.
num_channels() <<
" channel EST_Track\n";
565 cor[i] = correlation(a, b, i, i);
577 cerr <<
"Error: Couldn't find field named " << fname <<
584 cerr <<
"Error: Couldn't find field named " << fname <<
585 " in second Track\n";
593 cor[0] = correlation(a, b, ch_a, ch_b);
608 float *r =
new float[relax*3];
614 for (k = 0, j = Gof((i - relax), 0); j < i + relax + 1; ++j, ++k)
616 if (ref.
a(i, l) > 0.5)
617 r[k] = ((j < test.
num_frames()) && (test.
a(j, l)> 0.6)) ?1
620 r[k] = ((j < test.
num_frames()) && (test.
a(j, l)< 0.4)) ? -1
634 void align_to_track(
EST_Track &tr,
float &start,
float &end)
640 is = tr.
index(start);
650 void align_to_track(
EST_Track &tr,
int &start,
int &end,
int sample_rate)
652 float start_t = start/(float)sample_rate;
653 float end_t = end/(float)sample_rate;
657 align_to_track(tr, start_t, end_t);
660 start = (int)(start_t*sample_rate + 0.5);
661 end = (int)( end_t*sample_rate + 0.5);
665 int &start,
int &end,
669 float start_t = start/(float)sample_rate;
670 float end_t = end/(float)sample_rate;
674 int is = tr.
index(start_t-offset);
675 int ie = tr.
index(end_t-offset);
677 int start_s, start_c, start_e;
678 int end_s, end_c, end_e=0;
682 get_frame(tr, sample_rate, is, start_s, start_c, start_e);
683 get_frame(tr, sample_rate, ie, end_s, end_c, end_e);
687 start_s = (int)(tr.
t(is) * sample_rate);
688 end_s = (int)(tr.
t(ie) * sample_rate);
691 start = start_s + (int)(offset*sample_rate + 0.5);
692 end = end_e + (int)(offset*sample_rate + 0.5);
695 int nearest_boundary(
EST_Track &tr,
float time,
int sample_rate,
float offset)
699 float distance = 10000;
703 float start, center, end;
705 get_frame(tr, sample_rate, i, start, center, end);
709 if (fabs(start-time) > distance)
711 distance = fabs(start-time);
717 void move_start(
EST_Track &tr,
float shift)
723 void set_start(
EST_Track &tr,
float start)
725 float shift = start - tr.
t(0);
727 move_start(tr, shift);
735 from = orig.
index(start);
745 ret.
a(i, j) = orig.
a(i + from, j);
746 ret.
t(i) = orig.
t(i + from);
772 is = orig.
index(start);
773 ie = orig.
index(end);
776 new_num_frames = (ie - is) > 0 ?ie - is : 0;
779 for (i = 0; i < new_num_frames; ++i)
782 ret.
a(i, j) = orig.
a(i + is, j);
783 ret.
t(i) = orig.
t(i + is);
791 int get_order(
const EST_Track &t, EST_CoefficientType type,
int d)
794 EST_ChannelType start_c = (EST_ChannelType)EST_CoefChannelId(type, d, 0);
795 EST_ChannelType end_c = (EST_ChannelType)EST_CoefChannelId(type, d, 1);
810 EST_CoefficientType t;
812 for(t=cot_first; t <cot_free; t=(EST_CoefficientType)(t+1))
813 if ((order=get_order(tr,t))>0)
816 cout <<
"No coefficients in track\n";
822 int start_frame,
int end_frame)
831 for(
int i=start_frame; i<end_frame; i++)
832 l += (
int)t.
a(i, channel_length);
835 cout <<
"no length channel";
841 void get_start_positions(
const EST_Track &t,
int sample_rate,
848 cout <<
"no length channel\n";
854 int wstart, wcent, wend;
855 get_frame(t, sample_rate, i, wstart, wcent, wend);
870 from = al.
ival(
"-from");
881 tr.
sub_track(sub_track, from, to-from+1, 0, EST_ALL);
890 new_ch = ch_list.length();
895 for (i = 0, p = ch_list.head(); p; p = p->next(), ++i)
900 EST_error(
"Tried to extract channel number %d from track with "
904 nt.
a(j, i) = orig.
a(j, k);
918 int num_channels, num_frames;
921 for (num_channels=0,p=list.head(); p; p=p->next())
922 num_channels += list(p).num_channels();
924 if (style ==
"first")
926 num_frames = list.
first().num_frames();
927 longest = list.head();
931 if (style !=
"longest")
932 cerr <<
"EST_Track: unknown combine style \"" << style <<
933 "\" assuming longest" << endl;
934 for (num_frames = 0, longest = p = list.head(); p; p = p->next())
935 if (num_frames < list(p).num_frames())
937 num_frames = list(p).num_frames();
942 a.
resize(num_frames, num_channels);
945 for (k = 0, p = list.head(); p; p = p->next())
947 n = Lof(num_frames, list(p).num_frames());
948 for (j = 0; j < list(p).num_channels(); ++j, ++k)
950 for (i = 0; i < n; ++i)
951 a(i, k) = list(p).a(i, j);
956 for (i = 0; i < list(longest).num_frames(); ++i)
957 a.
t(i) = list(longest).t(i);
960 void channel_to_time(
EST_Track &tr,
int channel,
float scale)
965 tr.
t(i) = tr.
a(i,channel) * scale;
967 tr.set_equal_space(FALSE);
970 void channel_to_time(
EST_Track &tr, EST_ChannelType c,
float scale)
972 int channel = NO_SUCH_CHANNEL;
974 if (tr.map() != 0 && (channel = (tr.map()->
get(c)) != NO_SUCH_CHANNEL))
976 channel_to_time(tr, channel, scale);
981 cerr <<
"no channel '" << EST_default_channel_names.name(c) <<
"' = " << (int)c <<
"\n";
989 if (tr.channel_name(c) == c_name)
991 channel_to_time(tr, c, scale);
995 cerr <<
"no channel named '" << c_name <<
"'\n";
999 void channel_to_time_lengths(
EST_Track &tr,
int channel,
float scale)
1006 tt += tr.
a(i,channel) * scale;
1008 tr.set_equal_space(FALSE);
1011 void channel_to_time_lengths(
EST_Track &tr, EST_ChannelType c,
float scale)
1013 int channel = NO_SUCH_CHANNEL;
1015 if (tr.map()!=0 && (channel = tr.map()->
get(c)) != NO_SUCH_CHANNEL)
1017 channel_to_time_lengths(tr, channel, scale);
1022 cerr <<
"no channel '" << EST_default_channel_names.name(c) <<
"' = " << (int)c <<
"\n";
1030 if (tr.channel_name(c) == c_name)
1032 channel_to_time_lengths(tr, c, scale);
1036 cerr <<
"no channel named '" << c_name <<
"'\n";
1044 "-start <float> Extract track starting at this time, \n"
1045 " specified in seconds\n\n"
1046 "-end <float> Extract track ending at this time, \n"
1047 " specified in seconds\n\n"
1048 "-from <int> Extract track starting at this frame position\n\n"
1049 "-to <int> Extract track ending at this frame position\n\n";
1057 "-itype <string> Input file type (optional). If no type is\n"
1058 " specified type is automatically derived from\n"
1059 " file's header. Supported types\n"
1060 " are: "+options_track_filetypes()+
"\n\n"
1062 "-ctype <string> Contour type: F0, track\n\n"
1063 "-s <float> Frame spacing of input in seconds, for unheadered input file\n\n"
1064 "-startt <float> Time of first frame, for formats which don't provide this\n\n"
1065 "-c <string> Select a subset of channels (starts from 0). \n"
1066 " Tracks can have multiple channels. This option \n"
1067 " specifies a list of numbers, refering to the channel \n"
1068 " numbers which are to be used for for processing. \n\n"+
1078 "-otype <string> {ascii}\n"+
1079 " Output file type, if unspecified ascii is\n"+
1080 " assumed, types are: "+options_track_filetypes()+
", label\n\n"+
1081 "-S <float> Frame spacing of output in seconds. If this is \n"
1082 " different from the internal spacing, the contour is \n"
1083 " resampled at this spacing \n\n"
1084 "-o <ofile> Output filename, defaults to stdout\n\n";
1089 cout << t.
name() << endl;
1090 cout <<
"Number of frames: " << t.
num_frames() << endl;
1091 cout <<
"Number of channels: " << t.
num_channels() << endl;
1092 cout <<
"File type: " << EST_TrackFile::map.name(t.file_type()) << endl;
1094 cout <<
"Frame shift: " << t.
shift() << endl;
1096 cout <<
"Frame shift: varied" << endl;
1098 cout <<
"Channel: " << i <<
": " << t.channel_name(i) << endl;
1106 return EST_TrackFile::options_short();
1109 EST_String options_track_filetypes_long(
void)
1114 return EST_TrackFile::options_supported();