static public double dtwcost(MFCCFrame frame1, MFCCFrame frame2) { double tempSum = 0; for (int i = 0; i < 13; i++) { tempSum += ((frame1.Features[i] - frame2.Features[i]) * (frame1.Features[i] - frame2.Features[i])); } return(Math.Sqrt(tempSum)); }
/// <summary> /// Calculates the distance between two audio frames in the form of an MFCCFrame object /// returns the difference in a double /// </summary> /// double distance(MFCCFrame frame1, MFCCFrame frame2) { double tempSum = 0; for (int i = 0; i < 13; i++) { double var1 = (Math.Abs(frame1.Features[i] - frame2.Features[i])); //tempSum += Math.Pow(Math.Abs(frame1.Features[i] - frame2.Features[i]), 2); tempSum += (var1 * var1); } return(Math.Sqrt(tempSum)); }
private void btnIdentify_Click(object sender, EventArgs e) { /// <summary> /// updating status label /// </summary> /// metroLabel1.Visible = true; pruningToggle1.Visible = true; labelStatus.Text = "Status: Identifying speaker and running DTW algorithm..."; List <User> test = new List <User>(); string cnt = "";// To choose a testcase if (Testcase1_radioButton.Checked == true) { test = TestcaseLoader.LoadTestcase1Testing(@"C:\SpeakerID\Test Cases\Complete SpeakerID Dataset\TestingList.txt"); cnt = "1"; } else if (Testcase2_radioButton.Checked == true) { test = TestcaseLoader.LoadTestcase2Testing(@"C:\SpeakerID\Test Cases\Complete SpeakerID Dataset\TestingList.txt"); cnt = "2"; } else if (Testcase3_radioButton.Checked == true) { test = TestcaseLoader.LoadTestcase3Testing(@"C:\SpeakerID\Test Cases\Complete SpeakerID Dataset\TestingList.txt"); cnt = "3"; } else if (caseMilestone1.Checked) //First testcase { AudioSignal signalTemp = AudioOperations.OpenAudioFile("C:\\SpeakerID\\Test Cases\\Sample Test\\Input sample\\ItIsPlausible_Rich_US_English.wav"); signalTemp = AudioOperations.RemoveSilence(signalTemp); //don't analyze (template function) User userTemp = new User(); userTemp.UserTemplates = new List <AudioSignal>(); userTemp.UserName = ("Rich"); userTemp.UserTemplates.Add(signalTemp); test.Add(userTemp); cnt = "4"; } else if (caseMilestone2.Checked) { AudioSignal signalTemp = AudioOperations.OpenAudioFile("C:\\SpeakerID\\Test Cases\\Pruning Test\\1 min\\[Input] Why Study Algorithms - (1 min).wav"); signalTemp = AudioOperations.RemoveSilence(signalTemp); User userTemp = new User(); userTemp.UserTemplates = new List <AudioSignal>(); userTemp.UserName = ("BigOh (1 min)"); userTemp.UserTemplates.Add(signalTemp); test.Add(userTemp); cnt = "5"; } else if (caseMilestone3.Checked) { AudioSignal signalTemp = AudioOperations.OpenAudioFile("C:\\SpeakerID\\Test Cases\\Pruning Test\\4 min\\[Input] Why Study Algorithms - (4 min).wav"); signalTemp = AudioOperations.RemoveSilence(signalTemp); User userTemp = new User(); userTemp.UserTemplates = new List <AudioSignal>(); userTemp.UserName = ("BigOh (4 min)"); userTemp.UserTemplates.Add(signalTemp); test.Add(userTemp); cnt = "6"; } for (int y = 0; y < test.Count; ++y) { Random rnd = new Random(); //Generate random number to choose an audio sample to test it against the database AudioSignal sig = new AudioSignal(); sig = test[y].UserTemplates[0]; //rnd.Next(0, test[y].UserTemplates.Count)]; Sequence compareSequence_input = new Sequence(); compareSequence_input = AudioOperations.ExtractFeatures(sig); FileStream FS = new FileStream("C:/SpeakerID/case" + cnt + "/Relations.txt", FileMode.Open); StreamReader SR = new StreamReader(FS); string relations = SR.ReadLine(); SR.Close(); /// <summary> /// Split the string relations into an array of strings /// Delimiter: # /// Then split each string into name and path /// Delimiter: % /// Filling an array of "relationItem"s with the names and sequence paths of all sequences in the database, and leaving the /// distance attribute in all the array items as they are "NULL", until calculated using the DTW algorithm /// </summary> /// string[] relationsStrArr = relations.Split('#'); relationItem[] relationsStructArr = new relationItem[relationsStrArr.Length - 1]; matchedPerson person = new matchedPerson(); person.distance = -1; for (int i = 0; i < relationsStructArr.Length; i++) { string[] stringArrTemp = relationsStrArr[i].Split('%'); relationsStructArr[i].nameStr = stringArrTemp[0]; relationsStructArr[i].sequencePath = stringArrTemp[1]; } Stopwatch sw; TimeSpan TS = TimeSpan.Zero; for (int i = 0; i < relationsStructArr.Length; i++) //Loops on all sequences in DB { /// Reset the "compareSequence_output" variable and prepare SR for reading the sequence Sequence compareSequence_output = new Sequence(); FS = new FileStream(relationsStructArr[i].sequencePath, FileMode.Open); SR = new StreamReader(FS); /// Resetting the "file" variable string file = ""; file += SR.ReadLine(); string[] strTempFrames = file.Split('#'); //strTemp now contains all Frames in a sequence compareSequence_output.Frames = new MFCCFrame[strTempFrames.Length]; //Setting number of frames in output sequence for (int j = 0; j < strTempFrames.Length; j++) //Loops on all frames in a sequence { string[] strTempFeatures = strTempFrames[j].Split('%'); MFCCFrame frame = new MFCCFrame(); for (int k = 0; k < 13; k++) { frame.Features[k] = double.Parse(strTempFeatures[k]); //Loops on all 13 features in a frame and converts string to double } compareSequence_output.Frames[j] = new MFCCFrame(); for (int k = 0; k < 13; k++) { compareSequence_output.Frames[j].Features[k] = frame.Features[k]; } } /// Save the difference between compareSequence_output and compareSequence_input in relationsStructArr[i].distance if (pruningToggle1.Checked == true) { sw = Stopwatch.StartNew(); relationsStructArr[i].distance = Pruning_DTW(compareSequence_input, compareSequence_output); sw.Stop(); TS += sw.Elapsed; } else { sw = Stopwatch.StartNew(); relationsStructArr[i].distance = DTW_improved(compareSequence_input, compareSequence_output); sw.Stop(); TS += sw.Elapsed; } // update person object if (relationsStructArr[i].distance < person.distance || person.distance == -1) { person.distance = relationsStructArr[i].distance; person.name = relationsStructArr[i].nameStr; } /// Close the SR SR.Close(); Console.WriteLine(relationsStructArr[i].nameStr); } /// <summary> /// Updating status label and showing search results /// </summary> /// labelStatus.Text = "Status: Searching done."; Console.WriteLine(test[y].UserName); MessageBox.Show("Search completed\nThe closest match to this speaker is: " + person.name + "\nTime Elapsed for DTW only: " + TS.Hours + ':' + TS.Minutes + ':' + TS.Seconds + ':' + TS.Milliseconds + "\nTotal cost: " + person.distance, "Search Results"); labelStatus.Text = "Status: Ready"; } }