If you want to use the k-NN in conjunction with DTW, then it's quite easy. Assuming that you have the speech corpora of keywords you should do the following at the training stage:
- Each keyword should have more than one utterance recorded
- Extract the MFCC's for all the recordings and store them - these are effectively your "model data" - training samples.
Classification consists of:
Extract the matrix of MFCC's for the test sample
Calculate the DTW of the test sample with each and every MFCC matrix that was extracted and the training stage.
Store the final distance for each comparison. Now you should have a vector of total DTW distances (of a length equal to the number of the training examples):
test vs. class1_sample1: 10.2
test vs. class1_sample2: 20.3
test vs. class1_sample3: 15.1
test vs. class2_sample1: 3.1
test vs. class2_sample2: 2.5
...
For each of the classes (keywords), sort the distances (from small to large)
Pick the k top distances for each class and average them. These are the per-class memberships.
Predict the keyword by choosing the class with smallest distance.