From 39f35132412686a9a960ee3791052587e8bc8d43 Mon Sep 17 00:00:00 2001 From: Taylor Oxelgren Date: Tue, 10 Feb 2026 03:18:14 -0600 Subject: [PATCH] Implemented distance matrix and used openmp to scale accross cores --- README.md | 6 ++- src/main.c | 110 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 97 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 7577a2a..55a0002 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ 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 +With loudnorm to try to improve performance by normalizing loudness + + Seems to work much better `for %f in (*.wav) do ffmpeg -y -i "%f" -ar 44100 -lavfi "loudnorm,showspectrumpic=s=256x256:scale=log:legend=0" "img\%~nf.png"` @@ -14,7 +16,7 @@ With loudnorm to try to improve performance Used for gathering the image data ## Building -`clang src/main.c -Iexternal -O2 -o soundknn.exe` +`clang src/main.c -Iexternal -O3 -fopenmp -march=native -o soundknn.exe` ### Todo - [x] Load all images from directory diff --git a/src/main.c b/src/main.c index 0f110e0..0f95915 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" @@ -25,8 +27,69 @@ float EuclideanDistance(AudioData *im1,AudioData *im2){ distance += d * d; } + // Leaving out sqrt to save compute as it doesn't change distance rankings + // distance=sqrtf(distance); + return distance; +} - return sqrtf(distance); +void computeDistanceMatrix(AudioData *audioData, float **distanceArrays,int nfiles, int counter){ + // Computes distance matrix for all images + int matrixSize=nfiles*nfiles; + unsigned int computeCounter=0; + for(int i = 0; i < counter; i++){ + for(int j = 0; j < counter; j++){ + float d = EuclideanDistance(&audioData[i], &audioData[j]); + distanceArrays[i][j] = d; + computeCounter++; + } + // printf("Way through matrix compute: %d\n",(computeCounter/matrixSize)*100); + printf("\rWay through matrix compute: %.2f%%",(computeCounter/(float)matrixSize)*100); + fflush(stdout); + } + printf("\n"); +} + +void computeDistanceMatrixOMP(AudioData *audioData, float **distanceArrays,int nfiles, int counter){ + // Computes distance matrix for all images + int matrixSize=2000*2000; + unsigned int computeCounter=0; + // Uses openmp to use all threads for this + #pragma omp parallel for schedule(static) + for(int i = 0; i < counter; i++){ + for(int j = 0; j < counter; j++){ + float d = EuclideanDistance(&audioData[i], &audioData[j]); + distanceArrays[i][j] = d; + #pragma omp atomic + computeCounter++; + } + #pragma omp critical + { + printf("\rWay through matrix compute: %.2f%%",(computeCounter/(float)matrixSize)*100); + fflush(stdout); + } + } + printf("\n"); +} + + +int getAmountOfFiles(){ + HANDLE myHandle; + WIN32_FIND_DATA FindFileData; + const char* directory="esc-50-audio/img/*.png"; + int x,y; + int counter=0; + + // Getting first file out of the loop + myHandle=FindFirstFileA(directory,&FindFileData); + counter++; + + while(FindNextFileA(myHandle,&FindFileData)){ + counter++; + } + // Closing windows handler + FindClose(myHandle); + + return counter; } int main(){ @@ -36,7 +99,9 @@ int main(){ const char* directory="esc-50-audio/img/*.png"; int x,y; - struct AudioData audioData[2000]; + int nfiles=getAmountOfFiles(); + // Allocating on the heap to account for unknown number of images + struct AudioData *audioData = malloc(sizeof(struct AudioData) * nfiles); // Getting first file out of the loop myHandle=FindFirstFileA(directory,&FindFileData); @@ -48,7 +113,7 @@ int main(){ int counter=1; // Gets all other files - while(FindNextFileA(myHandle,&FindFileData)&& counter<2000){ + while(FindNextFileA(myHandle,&FindFileData)&& counter