Exemplo n.º 1
0
        public static Signal LoadARFFFile(string filepath)
        {
            Signal signal = null;

            try
            {
                Signal.Type type = Signal.Type.FLOAT;
                uint        dim  = 0;
                double      rate = 0;

                string[] lines  = File.ReadAllLines(filepath);
                char[]   delims = { ' ', '\t', ';', ',' };
                string[] tokens = lines[0].Split(delims);
                dim = (uint)tokens.Length;

                string[] row = null;

                row = lines[0].Split(delims);
                double time1 = double.Parse(row[1]);
                row = lines[1].Split(delims);
                double time2 = double.Parse(row[1]);

                double step = time2 - time1;
                rate = 1000.0 / (1000.0 * step);

                uint number = (uint)lines.Length;
                uint bytes  = Signal.TypeSize[(int)type];

                if (dim > 0)
                {
                    signal = new Signal(filepath, rate, 1, bytes, number, type);

                    StreamReader fs_data = new StreamReader(filepath);
                    LoadDataArff(signal, fs_data, dim - 1);
                    fs_data.Close();

                    signal.ShowDim = 0;
                    signal.loaded  = true;
                }
            }
            catch (Exception e)
            {
                MessageTools.Error(e.ToString());
                return(null);
            }

            return(signal);
        }
Exemplo n.º 2
0
        public static Signal LoadCSVFile(string filepath)
        {
            Signal signal = null;

            try
            {
                Signal.Type type = Signal.Type.UNDEF;
                uint        dim  = 0;
                double      rate = 0;

                if (SelectDataType(filepath, ref type, ref rate))
                {
                    string[] lines = File.ReadAllLines(filepath);

                    uint number = (uint)lines.Length;
                    uint bytes  = Signal.TypeSize[(int)type];

                    if (type != Signal.Type.UNDEF &&
                        rate > 0 &&
                        bytes > 0 &&
                        number > 0)
                    {
                        char[]   delims = { ' ', '\t', ';', ',' };
                        string[] tokens = lines[0].Split(delims);
                        dim = (uint)tokens.Length;

                        if (dim > 0)
                        {
                            signal = new Signal(filepath, rate, dim, bytes, number, type);

                            StreamReader fs_data = new StreamReader(filepath);
                            LoadDataV2a(signal, fs_data);
                            fs_data.Close();

                            signal.loaded = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageTools.Error(e.ToString());
                return(null);
            }

            return(signal);
        }
Exemplo n.º 3
0
 protected Signal()
 {
     rate      = 0;
     dim       = 0;
     bytes     = 0;
     time      = 0;
     number    = 0;
     data      = null;
     min       = null;
     max       = null;
     loaded    = false;
     isAudio   = false;
     type      = Signal.Type.UNDEF;
     meta_name = "";
     meta_num  = 0;
     meta_type = "";
 }
Exemplo n.º 4
0
        protected Signal(string filepath, double rate, uint dim, uint bytes, uint number, Signal.Type type, string meta_name = "", int meta_num = 0, string meta_type = "")
            : base()
        {
            this.filePath = filepath;
            string[] tmp = filepath.Split('\\');
            this.fileName  = tmp[tmp.Length - 1];
            this.name      = this.fileName.Split('.')[0];
            this.rate_real = this.rate = rate;
            this.dim       = dim;
            this.bytes     = bytes;
            this.number    = number;
            this.type      = type;

            this.meta_name = meta_name;
            this.meta_num  = meta_num;
            this.meta_type = meta_type;

            if (number > 0)
            {
                data = new float[number * dim];
            }
        }
Exemplo n.º 5
0
        public static bool SelectDataType(string filename, ref Signal.Type type, ref double rate)
        {
            SelectionBox box = new SelectionBox("Please select data type and sample rate (Hz) for:\r\n" + filename + "'", Signal.TypeName, "1", 9);

            box.ShowDialog();
            box.Close();

            if (box.DialogResult == true)
            {
                type = (Signal.Type)box.ComboBoxResult();
                try
                {
                    rate = double.Parse(box.TextFieldResult());
                    return(true);
                }
                catch (Exception e)
                {
                    MessageTools.Error(e.ToString());
                }
            }

            return(false);
        }
Exemplo n.º 6
0
        public static Signal LoadAudioFile(string filepath)
        {
            Signal signal = null;

            using (var reader = new AudioFileReader(filepath))
            {
                int    bytesPerSample = (reader.WaveFormat.BitsPerSample / 8);
                var    samples        = (uint)(reader.Length / (bytesPerSample));
                var    seconds        = reader.TotalTime.TotalSeconds;
                double rate           = reader.WaveFormat.SampleRate;
                uint   dimension      = (uint)reader.WaveFormat.Channels;

                Signal.Type type = (bytesPerSample == 2)  ? Signal.Type.SHORT : Signal.Type.FLOAT;
                signal = new Signal(filepath, rate, dimension, (uint)bytesPerSample, (samples / dimension), type);
                reader.Read(signal.data, 0, (int)samples);

                signal.minmax();
                signal.isAudio = true;
                signal.loaded  = true;
            }

            return(signal);
        }
Exemplo n.º 7
0
        //shrink konsturktor
        public Signal(Signal s, UInt32 width, double fromInSec, double toInSec)
        {
            this.dim      = s.dim;
            this.bytes    = s.bytes;
            this.time     = s.time;
            this.fileName = s.fileName;
            this.filePath = s.filePath;
            this.name     = s.name;
            this.type     = s.type;
            this.isAudio  = s.isAudio;

            this.meta_name = "";
            this.meta_num  = 0;
            this.meta_type = "";

            uint fromInSamples = (uint)Math.Round(fromInSec * s.rate);
            uint toInSamples   = (uint)Math.Round(toInSec * s.rate);
            uint lenInSamples  = toInSamples - fromInSamples;

            uint fromAvailableInSamples = Math.Min(s.number, fromInSamples);
            uint toAvailableInSamples   = Math.Min(s.number, toInSamples);
            uint lenAvailableInSamples  = toAvailableInSamples - fromAvailableInSamples;

            if (lenAvailableInSamples == 0)
            {
                this.rate   = 0;
                this.number = 0;
                this.data   = null;
            }

            double step = (double)lenInSamples / (double)width;

            if (step > 1.0)
            {
                //Resample(s.number, from, to, this.number, this.dim, s.data, this.data);

                this.number    = width;
                this.data      = new float[this.number * this.dim];
                this.rate      = s.rate / step;
                this.rate_real = s.rate_real;

                double step_sum = 0;
                uint   pos_to   = 0;
                uint   pos_from = 0;
                for (uint i = 0; i < this.number; i++)
                {
                    pos_from = fromInSamples + ((uint)step_sum);
                    if (pos_from >= s.number)
                    {
                        for (; i < this.number; i++)
                        {
                            for (uint j = 0; j < this.dim; j++)
                            {
                                this.data[pos_to++] = float.NaN;
                            }
                        }
                        break;
                    }

                    pos_from *= this.dim;
                    for (uint j = 0; j < this.dim; j++)
                    {
                        this.data[pos_to++] = s.data[pos_from + j];
                    }
                    step_sum += step;
                }
            }
            else
            {
                this.rate      = s.rate;
                this.rate_real = s.rate_real;
                this.number    = lenInSamples;

                data = new float[lenInSamples * dim];
                int  index  = 0;
                uint offset = fromInSamples * dim;
                for (uint n = 0; n < lenInSamples; n++)
                {
                    if (offset + index >= s.number * dim)
                    {
                        for (; n < lenInSamples; n++)
                        {
                            for (uint j = 0; j < this.dim; j++)
                            {
                                this.data[index++] = float.NaN;
                            }
                        }
                        break;
                    }
                    for (uint d = 0; d < dim; d++)
                    {
                        data[index] = s.data[offset + index];
                        index++;
                    }
                }
            }

            minmax();
            loaded = true;
        }