private void parseDataRecordStream(StreamReader sr) { //set the seek position in the file stream to the beginning of the data records. sr.BaseStream.Seek((256 + this.Header.NumberOfSignalsInDataRecord * 256), SeekOrigin.Begin); int dataRecordSize = 0; foreach (EDFSignal signal in this.Header.Signals) { signal.SamplePeriodWithinDataRecord = this.Header.DurationOfDataRecordInSeconds / signal.NumberOfSamplesPerDataRecord; dataRecordSize += signal.NumberOfSamplesPerDataRecord; } byte[] dataRecordBytes = new byte[dataRecordSize * 2]; while (sr.BaseStream.Read(dataRecordBytes, 0, dataRecordSize * 2) > 0) { EDFDataRecord dataRecord = new EDFDataRecord(); int j = 0; int samplesWritten = 0; foreach (EDFSignal signal in this.Header.Signals) { List <float> samples = new List <float>(); for (int l = 0; l < signal.NumberOfSamplesPerDataRecord; l++) { float value = (float)(((BitConverter.ToInt16(dataRecordBytes, (samplesWritten * 2)) + (int)signal.Offset)) * signal.AmplifierGain); samples.Add(value); samplesWritten++; } dataRecord.Add(signal.IndexNumberWithLabel, samples); j++; } _dataRecords.Add(dataRecord); } }
public void LoadFromEdfFile(EDFFile file, EDFSignal signal, EDFSignal reference) { Label = signal.Label; ReferenceLabel = reference?.Label ?? "mono"; SampleRate = signal.NumberOfSamplesPerDataRecord / file.Header.DurationOfDataRecordInSeconds; int numberOfSamples = signal.NumberOfSamplesPerDataRecord * file.Header.NumberOfDataRecords; Data = new float[numberOfSamples]; ViewAmplitude = 0.005F; for (int i = 0; i < Data.Length; i++) { EDFDataRecord record = file.DataRecords[i / signal.NumberOfSamplesPerDataRecord]; float[] recordData = record[signal.IndexNumber]; if (reference == null) { Data[i] = recordData[i % signal.NumberOfSamplesPerDataRecord]; } else { EDFDataRecord refRecord = file.DataRecords[i / signal.NumberOfSamplesPerDataRecord]; float[] refRecordData = refRecord[reference.IndexNumber]; Data[i] = recordData[i % reference.NumberOfSamplesPerDataRecord] - refRecordData[i % reference.NumberOfSamplesPerDataRecord]; } } }
// EDF File Parsing /// <summary> /// Modified From EDF library function https://edf.codeplex.com/SourceControl/latest#trunk/EDFFile.cs /// Gets the signal samples from one period of time to another /// </summary> /// <param name="file"> The EDFFile class </param> /// <param name="signal_to_retrieve"> The signal to get samples from </param> /// <param name="StartTime"> The start time to get samples from </param> /// <param name="EndTime"> The end time to get samples from </param> /// <returns> A list of the retrieved samples </returns> public static List <float> retrieveSignalSampleValuesMod(EDFFile file, EDFSignal signal_to_retrieve, DateTime StartTime, DateTime EndTime) { int start_sample, start_record; int end_sample, end_record; #region Find Start and End Points // Duration of record in seconds double record_duration = file.Header.DurationOfDataRecordInSeconds; // Samples per record double samples_per_record = signal_to_retrieve.NumberOfSamplesPerDataRecord; // The sample period of the signal (Duration of Record)/(Samples per Record) double sample_period = record_duration / samples_per_record; { // Time of start point in seconds double total_seconds = (StartTime - file.Header.StartDateTime).TotalSeconds; // Time of start point in samples double total_samples = total_seconds / sample_period; start_sample = ((int)(total_samples)) % ((int)samples_per_record); // Start Sample in Record start_record = (int)((total_samples - start_sample) / samples_per_record); // Start Record } { // Time of end point in seconds double total_seconds = (EndTime - file.Header.StartDateTime).TotalSeconds; // Time of end point in samples double total_samples = total_seconds / sample_period - 1; end_sample = ((int)total_samples) % ((int)samples_per_record); // End Sample in Record end_record = (((int)total_samples) - end_sample) / ((int)samples_per_record); // End Record } #endregion List <float> signalSampleValues = new List <float>(); if (file.Header.Signals.Contains(signal_to_retrieve)) { for (int x = start_record; x <= end_record; x++) { EDFDataRecord dr = file.DataRecords[x]; foreach (EDFSignal signal in file.Header.Signals) { if (signal.IndexNumberWithLabel.Equals(signal_to_retrieve.IndexNumberWithLabel)) { int start = x == start_record ? start_sample : 0; int end = x == end_record ? end_sample : dr[signal.IndexNumberWithLabel].Count - 1; for (int y = start; y <= end; y++) { signalSampleValues.Add(dr[signal.IndexNumberWithLabel][y]); } } } } } return(signalSampleValues); }
/// <summary> /// 解析数据 /// </summary> /// <param name="sr"></param> private void parseDataRecordStream(StreamReader sr) { //set the seek position in the file stream to the beginning of the data records. sr.BaseStream.Seek((256 + this.Header.NumberOfSignalsInDataRecord * 256), SeekOrigin.Begin); int dataRecordSize = 0; foreach (EDFSignal signal in this.Header.Signals) { signal.SamplePeriodWithinDataRecord = (float)(this.Header.DurationOfDataRecordInSeconds / signal.NumberOfSamplesPerDataRecord); dataRecordSize += signal.NumberOfSamplesPerDataRecord; } byte[] dataRecordBytes = new byte[dataRecordSize * 2]; while (sr.BaseStream.Read(dataRecordBytes, 0, dataRecordSize * 2) > 0) { EDFDataRecord dataRecord = new EDFDataRecord(); int j = 0; int samplesWritten = 0; foreach (EDFSignal signal in this.Header.Signals) { float refVoltage = signal.PhysicalMaximum; List <float> samples = new List <float>(); for (int l = 0; l < signal.NumberOfSamplesPerDataRecord; l++) { // float value = (float)(((BitConverter.ToInt16(dataRecordBytes, (samplesWritten * 2)) + (int)signal.Offset)) * signal.AmplifierGain); float value = (float)BitConverter.ToInt16(dataRecordBytes, (samplesWritten * 2)); if (this.Header.Version.Equals(NEURO_BOND_FLAG)) { if (value >= 0 && value <= Math.Pow(2, 19) - 1) { value = refVoltage * value / (float)(Math.Pow(2, 19) - 1); } else { //value = refVoltage * ((value - (float)Math.Pow(2, 20)) / (float)(Math.Pow(2, 19) - 1)); value = refVoltage * value / (float)Math.Pow(2, 19); } } value /= multiplyingPower; samples.Add(value); samplesWritten++; } dataRecord.Add(signal.IndexNumberWithLabel, samples); j++; } _dataRecords.Add(dataRecord); } }