42#include "EST_cutils.h"
43#include "EST_simplestats.h"
46#include "EST_TrackFile.h"
47#include "EST_Option.h"
48#include "EST_track_aux.h"
60static 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);
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);
100 int n = (int)(
x / c.
shift());
103 simple_mean_smooth(c, n,
j);
106void 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];
162void 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];
206 for (i = 0; i <
tr.num_frames(); ++i)
207 for (
j = 0;
j <
tr.num_channels(); ++
j)
215 meansd(
tr, mean,
sd);
216 normalise(
tr, mean,
sd, -1.0, 1.0);
232 for (
int i = 0; i <
tr.num_channels(); ++i)
236void normalise(
EST_Track &
tr,
float mean,
float sd,
int channel,
243 for (i = 0; i <
tr.num_frames(); ++i)
244 if (!
tr.track_break(i))
245 tr.a(i, channel) = ((((
tr.a(i, channel) - mean) / (4 *
sd)) + 0.5)
264 for (i = 0; i <
diff.num_frames(); ++i)
266 dist = c.
t(i + 1) - c.
t(i);
267 for (
j = 0;
j <
diff.num_channels(); ++
j)
269 : (c.a(i + 1) - c.a(i)) /
dist;
287 " channel EST_Track with " << b.
num_channels() <<
" channel EST_Track\n";
291 for (i = 0; i < size; ++i)
305 for (i = 0; i < size; ++i)
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);
340 EST_error(
"Tried to access channel %d of %d channel track",
341 channel,
tr.num_channels() );
348 if( !
tr.track_break(i) ){
349 mean +=
tr.a_no_check( i, channel );
353 return mean/(
float)n;
363 m.a_no_check(i) = mean(
tr, i );
374 m = mean(
tr, channel );
379 if( !
tr.track_break(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)
440 error = predict-real;
443 xx += predict*predict;
448 cor = (
xy.mean() - (
x.mean()*
y.mean()))/
449 (
sqrt(
xx.mean()-(
x.mean()*
x.mean())) *
450 sqrt(
yy.mean()-(
y.mean()*
y.mean())));
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);
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));
506 m.resize(
tl.first().num_channels());
507 sd.resize(
tl.first().num_channels());
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";
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)
634void align_to_track(
EST_Track &
tr,
float &start,
float &end)
640 is =
tr.index(start);
650void align_to_track(
EST_Track &
tr,
int &start,
int &end,
int sample_rate)
660 start = (int)(
start_t*sample_rate + 0.5);
661 end = (int)(
end_t*sample_rate + 0.5);
665 int &start,
int &end,
680 if (
tr.has_channel(channel_length))
691 start =
start_s + (int)(offset*sample_rate + 0.5);
692 end =
end_e + (int)(offset*sample_rate + 0.5);
695int nearest_boundary(
EST_Track &
tr,
float time,
int sample_rate,
float offset)
699 float distance = 10000;
701 for (
int i = 0; i <
tr.num_frames(); ++i)
705 get_frame(
tr, sample_rate, i, start,
center, end);
709 if (
fabs(start-time) > distance)
711 distance =
fabs(start-time);
714 return tr.num_frames();
719 for(
int i=0; i<
tr.num_frames(); i++)
725 float shift = start -
tr.t(0);
727 move_start(
tr, shift);
735 from =
orig.index(start);
736 to =
orig.index_below(end);
740 ret.resize(to - from,
orig.num_channels());
742 for (i = 0; i <
ret.num_frames(); ++i)
743 for (
j = 0;
j <
ret.num_channels(); ++
j)
747 if (
orig.track_break(i + from))
772 is =
orig.index(start);
781 for (
j = 0;
j <
orig.num_channels(); ++
j)
784 if (
orig.track_break(i + is))
791int 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";
832 l += (
int)t.
a(i, channel_length);
835 cout <<
"no length channel";
841void get_start_positions(
const EST_Track &t,
int sample_rate,
848 cout <<
"no length channel\n";
867 if (
al.present(
"-start"))
868 from =
tr.index(
al.fval(
"-start"));
869 else if (
al.present(
"-from"))
870 from =
al.ival(
"-from");
874 if (
al.present(
"-end"))
875 to =
tr.index(
al.fval(
"-end"));
876 else if (
al.present(
"-to"))
879 to =
tr.num_frames() - 1;
881 tr.sub_track(sub_track, from, to-from+1, 0, EST_ALL);
895 for (i = 0, p =
ch_list.head(); p; p = p->next(), ++i)
899 if (k >=
orig.num_channels())
900 EST_error(
"Tried to extract channel number %d from track with "
901 "only %d channels\n", k,
orig.num_channels());
903 for (
j = 0;
j <
orig.num_frames(); ++
j)
905 nt.set_channel_name(
orig.channel_name(k), i);
907 for (
j = 0;
j <
orig.num_frames(); ++
j)
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();
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)
963 for(
int i=0; i <
tr.num_frames(); i++)
967 tr.set_equal_space(FALSE);
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";
988 for (
int c=0; c<
tr.num_channels(); c++)
995 cerr <<
"no channel named '" <<
c_name <<
"'\n";
1002 for(
int i=0; i <
tr.num_frames(); i++)
1008 tr.set_equal_space(FALSE);
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";
1029 for (
int c=0; c<
tr.num_channels(); c++)
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, referring 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";
1092 cout <<
"File type: " << EST_TrackFile::map.name(t.file_type()) <<
endl;
1096 cout <<
"Frame shift: varied" <<
endl;
1098 cout <<
"Channel: " << i <<
": " << t.channel_name(i) <<
endl;
1106 return EST_TrackFile::options_short();
1114 return EST_TrackFile::options_supported();
const T & first() const
return const reference to first item in list
int channel_position(const char *name, int offset=0) const
void set_channel_name(const EST_String &name, int channel)
set the name of the channel.
int track_break(int i) const
return true if frame i is a break
float & t(int i=0)
return time position of frame i
int val(int i) const
return true if frame i is a value
float & a(int i, int c=0)
bool has_channel(const char *name) const
bool equal_space() const
return true if track has equal (i.e. fixed) frame spacing */
int num_channels() const
return number of channels in track
int num_frames() const
return number of frames in track
void resize(int num_frames, int num_channels, bool preserve=1)
EST_String name() const
name of track - redundant use access to features