Implemented Knn, updated readme

This commit is contained in:
2026-02-10 00:51:52 -06:00
parent 6ea46ce8d9
commit 2f86f5fd99
2 changed files with 52 additions and 5 deletions

View File

@@ -2,8 +2,14 @@
Run knn on audio data which has been converted to images to find similar audio clips
## Ffmpeg preporcessing command to create spectrogram image
Spectogramspectrogram
`for %f in (*.wav) do ffmpeg -y -i "%f" -ar 44100 -lavfi "showspectrumpic=s=256x256:scale=log:legend=0" "img\%~nf.png"`
With loudnorm to try to improve performance
`for %f in (*.wav) do ffmpeg -y -i "%f" -ar 44100 -lavfi "loudnorm,showspectrumpic=s=256x256:scale=log:legend=0" "img\%~nf.png"`
## stb_image.h
Used for gathering the image data
@@ -12,5 +18,5 @@ Used for gathering the image data
### Todo
- [x] Load all images from directory
- [ ] Do knn algorithm
- [x] Do knn algorithm
- [ ] Use opencl to accelerate algorithm execution

View File

@@ -1,14 +1,33 @@
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
struct AudioData{
typedef struct AudioData{
unsigned char *data;
char *fileName;
};
}AudioData;
float EuclideanDistance(AudioData *im1,AudioData *im2){
// Using hard coded x,y for now
int x,y;
x=256;
y=256;
float distance=0;
for(int i=0;i<(x*y);i++){
float d = (float)im1->data[i] - (float)im2->data[i];
distance += d * d;
}
return sqrtf(distance);
}
int main(){
@@ -21,7 +40,6 @@ int main(){
// Getting first file out of the loop
myHandle=FindFirstFileA(directory,&FindFileData);
printf("%s\n",FindFileData.cFileName);
char path[MAX_PATH];
snprintf(path, MAX_PATH, "esc-50-audio/img/%s", FindFileData.cFileName);
audioData[0].data = stbi_load(path, &x, &y, NULL, 1);
@@ -29,8 +47,9 @@ int main(){
audioData[0].fileName = _strdup(path);
int counter=1;
// Gets all other files
while(FindNextFileA(myHandle,&FindFileData)&& counter<2000){
printf("%s\n",FindFileData.cFileName);
// printf("%s\n",FindFileData.cFileName);
snprintf(path, MAX_PATH, "esc-50-audio/img/%s", FindFileData.cFileName);
audioData[counter].data = stbi_load(path, &x, &y, NULL, 1);
audioData[counter].fileName = _strdup(path);
@@ -44,7 +63,29 @@ int main(){
distanceArrays[i]=calloc(2000,sizeof(float));
}
// Searches for index of specific image
char img[]="esc-50-audio/img/1-32318-A-0.png";
int fileindex;
for(int i=0;i<counter;i++){
if(strcmp(img,audioData[i].fileName)==0){
fileindex=i;
break;
}
}
float distance=EuclideanDistance(&audioData[fileindex],&audioData[0]);
int closest=fileindex;
for(int i=0;i<counter;i++){
if(strcmp(img,audioData[i].fileName)!=0){
float newdistance=EuclideanDistance(&audioData[fileindex],&audioData[i]);
if(newdistance<distance){
closest=i;
distance=newdistance;
}
}
}
printf("Closest : %s",audioData[closest].fileName);
// Freeing the memory of images
for(int i=0;i<counter;i++){