private void DiagnoseButton_Click(object sender, RoutedEventArgs e) { MLApp.MLApp matlab = new MLApp.MLApp(); matlab.Execute(@"cd " + Directory.GetCurrentDirectory()); DBHandler conn = new DBHandler(); conn.InitializeConnection(); List <string> ecgFilenames = conn.GetPatientEcgFilenames(Patient); List <DbAnswer> patientAnswers = conn.GetPatientAnswers(Patient.Id); string filename = ecgFilenames.First(); conn.CloseConnection(); if (filename != null) { object result; matlab.Feval("ECG_diagnosis", 5, out result, filename, 500.0); object[] res = result as object[]; double[][] ecgMoments = new double[4][]; for (int i = 1; i < 5; i++) { if (res[i] is System.Reflection.Missing) { ecgMoments[i - 1] = new double[0]; } else { double[,] moments = (double[, ])res[i]; ecgMoments[i - 1] = moments.Cast <double>().ToArray(); } } int[][] questionIds = { new int[] { 1, 2, 5, 8, 11, 15 }, new int[] { 1, 2, 3, 4, 5,8, 9, 11, 15 }, new int[] { 3, 4, 5, 8,15 }, new int[] { 1, 3, 5, 8, 11, 15 } }; double maxScore = 0.0; int diagnosis = -1; for (int i = 0; i < ecgMoments.Length; i++) { double score = 0.0; if (ecgMoments[i].Length > 0) { foreach (DbAnswer answer in patientAnswers) { if (questionIds[i].Contains(answer.QuestionId) && answer.Answer.ToLower().Equals("tak")) { score++; } } score = score * 10 / questionIds[i].Length; if (i <= 1 && Patient.Age >= 40) { score += (Patient.Age - 40) / 10.0; } score += ecgMoments[i].Length; } if (score > maxScore) { maxScore = score; diagnosis = i; } } EcgCharacteristics ecgCharacteristics = new EcgCharacteristics((double)res[0], ecgMoments[0], ecgMoments[1], ecgMoments[2], ecgMoments[3]); string diagnose = string.Format("Average Heart Rythm: {0}\nBradycardia: {1}\nTachycardia: {2}\nPremature Ventricular Contraction: {3}\nPremature Atrium Contraction: {4}", (double)res[0], ecgMoments[0].Length, ecgMoments[1].Length, ecgMoments[2].Length, ecgMoments[3].Length); this.Diagnoza.Text = diagnose; } }
/// <summary> /// thread for faster analysing /// </summary> void run() { // skip if file not available if (!FileHandler.available()) { return; } progressBar1.Value = 0; // load sample rate sampleRate = FileHandler.readSampleRate(); // load samples samples = FileHandler.load(slider1); progressBar1.Value = 20; Application.DoEvents(); bool fst = false; // if signal was ecg if (decg.Checked) { // set samples graph1.samples = samples; graph1.sampleRate = sampleRate; graph1.ylabel = "mv"; } else { graph1.samples = new SignalSamples(); graph1.samples.sampleRate = 0; graph1.sampleRate = 0; bool stop = false; // initialize ecg arrays int[] tr = new int[3]; int[] offset = new int[3]; List <float>[] sigz = new List <float> [3]; EcgCharacteristics[] last = new EcgCharacteristics[3]; EcgCharacteristics[] now = new EcgCharacteristics[3]; // define ecg arrays for (int i = 0; i < 3; i++) { sigz[i] = new List <float>(); offset[i] = sampleRate; // detect first ecg signal last[i] = samples.FindDelta(i + 1, 0, offset[i]); tr[i] = 0; } // loop until signal is end do { for (int i = 0; i < 3; i++) { // detect second ecg signal now[i] = samples.FindDelta(i + 1, last[i].lastCharacteristic(), offset[i]); // find distrance between two pulses offset[i] = Math.Abs(now[i]["R"].idx - last[i]["R"].idx); // if the heart beat(pulse rate) between exact range if (offset[i] > 0 && offset[i] < sampleRate) { // find heart beat sample rate if (!fst) { graph1.samples.sampleRate += offset[i]; if (i == 2) { fst = true; } } float hpr = 0; // calculate ST line if (dst.Checked) { hpr = (now[i]["ST"].value - now[i]["Q"].value); } else if (dpr.Checked) // calculate heart beat { hpr = 60.0f / (float)(offset[i] / (float)sampleRate); } sigz[i].Add(hpr); } else { // if pulse rate was not reliable set offset as default sample rate offset[i] = sampleRate; } // if ecg data was not reliable try again if (now[i]["c"].idx <= 0 || now[i]["T"].idx <= 0) { tr[i]++; } // set last ecg signal if signal was out of range if (now[i].lastCharacteristic() <= last[i].lastCharacteristic()) { now[i]["c"].idx = last[i].lastCharacteristic() + offset[i]; } // skip if signal was out of range if (now[i].lastCharacteristic() + offset[i] > samples.Count) { tr[i] = 40; } // set last[i] = now[i]; } // return when data was end if (tr[0] > 30 && tr[1] > 30 && tr[2] > 30) { stop = true; } } while (!stop); // find maximum signal range int max = sigz[0].Count; for (int i = 0; i < 3; i++) { if (sigz[i].Count > max) { max = sigz[i].Count; } } // insert collected data as samples for (int i = 0; i < max; i++) { Signal sig = new Signal(); for (int j = 0; j < 3; j++) { if (i < sigz[j].Count) { sig[j + 1] = sigz[j][i]; } } graph1.samples.Add(sig); } // show results if (dpr.Checked) { graph1.ylabel = ""; } else if (dst.Checked) { graph1.ylabel = "mv"; } progressBar1.Value = 100; graph1.samples.sampleRate = (int)((float)sampleRate / (float)graph1.samples.sampleRate / 3.0f + 1); graph1.sampleRate = graph1.samples.sampleRate; graph1.samples.NoiseReduction(3); } graph1.zoom = (float)graph1.samples.Count / (float)graph1.sampleRate; graph1.Refresh(); working = false; }