Using KNN to realize Voise Control

Dependencies:   mbed-os-lychee

Files at this revision

API Documentation at this revision

Comitter:
jianger_88
Date:
Fri Feb 07 03:06:22 2020 +0000
Parent:
0:61014bfdf244
Commit message:

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 61014bfdf244 -r 2fa6911afef3 main.cpp
--- a/main.cpp	Thu Apr 19 09:29:26 2018 +0000
+++ b/main.cpp	Fri Feb 07 03:06:22 2020 +0000
@@ -2,9 +2,12 @@
 #include "SdUsbConnect.h"
 #include "AUDIO_GRBoard.h"
 
+
 #define MOUNT_NAME             "storage"
 #define FINE_PATH              "/"MOUNT_NAME"/wav_rec.wav"
 
+
+
 #define SAMPLE_RATE            (8000)
 #define AUDIO_IN_BUF_SIZE      (2048)
 #define AUDIO_IN_BUF_NUM       (16)
@@ -16,9 +19,14 @@
 #define INFO_TYPE_CLOSE        (1)
 #define INFO_TYPE_WRITE_DATA   (2)
 
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
 AUDIO_GRBoard audio(0x80, AUDIO_OUT_BUF_NUM - 1, AUDIO_IN_BUF_NUM); // I2S Codec
-DigitalOut  led_rec(LED_RED);
-DigitalOut  led_play(LED_GREEN);
+int led_rec;
+int led_play;
 InterruptIn button_0(USER_BUTTON0);
 #if defined(TARGET_GR_LYCHEE)
 InterruptIn button_1(USER_BUTTON1);
@@ -128,6 +136,7 @@
     }
 }
 
+
 static void callback_audio_write_end(void * p_data, int32_t result, void * p_app_data) {
     disp_audio_wave((int16_t *)p_data, result / 2, 0x07E0); // Green
 }
@@ -156,6 +165,73 @@
     play_req = true;
 }
 
+
+
+static void KNN(char *file_name, int dat_size)
+{
+    double *cal_dat;
+    double *com_dat;
+    int record;
+    int NUM_LIB=119;
+    int class_num;
+    double knn=10000000000;
+    double temp=0;
+    com_dat=(double*)malloc(sizeof(double)*dat_size);
+    
+    FILE *input_res,*compare_res;
+    char *lib_name;
+    input_res=fopen(file_name,"rb");
+    for(int i=0;i<dat_size;i++)
+    {
+        fscanf(input_res,"lf",&cal_dat[i]);
+    }
+    fclose(input_res);
+    
+    for(int i=0;i<NUM_LIB;i++)
+    {
+        temp=0;
+        sprintf(lib_name,"sample%03d.dat",i);
+        compare_res=fopen(lib_name,"w");
+        for(int i=0;i<dat_size;i++){
+            fscanf(compare_res,"lf",&com_dat[i]);
+        }
+        for(int i=0;i<dat_size;i++)
+        {
+            temp+=pow((cal_dat[i]-com_dat[i]),2);
+        }
+        if (temp<knn)
+        {
+            knn=temp;
+            record=i;
+        }
+    }
+    if(0<record||record<=25||115<record||record<=119)
+    //output is bird
+    {
+    class_num=1;
+    led1=!led1;
+                        }
+    //output is monkey
+    else if(25<record||record<=54)
+    {
+    class_num=2;
+    led2=!led2;}
+    //output is cat
+    else if(54<record||record<=85||record==115)
+    {
+    class_num=3;
+    led3=!led3;
+                        }
+    //output is dog
+    else if(85<record||record<=114)
+    {class_num=4;
+     led4=!led4;
+                        }
+
+}
+
+
+
 static void callback_audio_read_end(void * p_data, int32_t result, void * p_app_data) {
 #if MBED_CONF_APP_LCD
     if (led_play == 0) {
@@ -249,6 +325,9 @@
 }
 
 int main() {
+    
+int count=0;
+
 #if MBED_CONF_APP_LCD
     rbsp_data_conf_t audio_write_data = {&callback_audio_write_end, NULL};
 
@@ -287,23 +366,53 @@
             uint32_t index = 0;
 
             play_req = false;
+#if(0)
+            //KNN(FINE_PATH,);
+            char test_name[32];
+            sprintf(test_name,"/"MOUNT_NAME"/sample%03d.dat",count++);
+            FILE *test_lib=fopen(test_name,"w");
+             fseek(test_lib, sizeof(wav_header_tbl), SEEK_SET);
+                while (read_size == AUDIO_OUT_BUF_SIZE) {
+                    read_size = fread(audio_out_buf[index], sizeof(char), AUDIO_OUT_BUF_SIZE, test_lib);
+            fclose(test_name);
+#else
             led_play = 1;
             FILE * fp_rb = fopen(FINE_PATH, "rb");
+
+            char test_name[32];
+ //           sprintf(test_name,"/"MOUNT_NAME"/sample%03d.dat",count++);
+            sprintf(test_name,"/"MOUNT_NAME"/recogniaze.dat");
+            FILE *test_lib=fopen(test_name,"w");
+
+
             if (fp_rb != NULL) {
                 fseek(fp_rb, sizeof(wav_header_tbl), SEEK_SET);
                 while (read_size == AUDIO_OUT_BUF_SIZE) {
                     read_size = fread(audio_out_buf[index], sizeof(char), AUDIO_OUT_BUF_SIZE, fp_rb);
-                    audio.write(audio_out_buf[index], read_size, &audio_write_data);
+                    
+   //                audio.write(audio_out_buf[index], read_size, &audio_write_data);
+
+                   fwrite(audio_out_buf[index], sizeof(char), read_size, test_lib);
+                   
+
+                    
                     index++;
                     if (index >= AUDIO_OUT_BUF_NUM) {
                         index = 0;
                     }
                 }
                 fclose(fp_rb);
-                Thread::wait(AUDIO_OUT_WAIT);
+                fclose(test_lib);
+                KNN("/"MOUNT_NAME"/recogniaze.dat",5000);
+               
+ //               Thread::wait(AUDIO_OUT_WAIT);
             }
             led_play = 0;
+#endif
         }
         Thread::wait(100);
+              
+
     }
 }
+