wavelib/test/denoisetest.c

140 lines
3.3 KiB
C
Raw Normal View History

2017-08-07 09:10:11 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
2017-09-24 20:54:45 +08:00
#include "../header/wauxlib.h"
2017-08-07 09:10:11 +08:00
2017-09-19 19:38:33 +08:00
static double rmse(int N,double *x,double *y) {
double rms;
int i;
rms = 0.0;
for(i = 0; i < N;++i) {
rms += (x[i] - y[i]) * (x[i] - y[i]);
}
rms = sqrt(rms/(double)N);
return rms;
}
static double corrcoef(int N,double *x,double *y) {
double cc,xm,ym,tx,ty,num,den1,den2;
int i;
xm = ym = 0.0;
for(i = 0; i < N;++i) {
xm += x[i];
ym += y[i];
}
xm = xm/N;
ym = ym / N;
num = den1 = den2 = 0.0;
for(i = 0; i < N;++i) {
tx = x[i] - xm;
ty = y[i] - ym;
num += (tx*ty);
den1 += (tx*tx);
den2 += (ty*ty);
}
cc = num / sqrt(den1*den2);
return cc;
}
2017-08-07 09:10:11 +08:00
int main() {
// gcc -Wall -I../header -L../Bin denoisetest.c -o denoise -lwauxlib -lwavelib -lm
2017-09-19 19:38:33 +08:00
double *sig,*inp,*oup;
2017-08-12 19:50:25 +08:00
int i,N,J;
2017-09-24 20:54:45 +08:00
FILE *ifp;
denoise_object obj;
2017-08-07 21:05:23 +08:00
double temp[2400];
2017-08-07 09:10:11 +08:00
char *wname = "db5";
char *method = "dwt";// Available - dwt, swt and modwt. modwt works only with modwtshrink. The other two methods work with
// visushrink and sureshrink
char *ext = "sym";// sym and per work with dwt. swt and modwt only use per extension when called through denoise.
// You can use sy extension if you directly call modwtshrink with cmethod set to fft. See modwtdenoisetest.c file
char *thresh = "soft";// soft or hard
char *level = "all"; // noise estimation at "first" or "all" levels. modwt only has the option of "all"
2017-08-07 21:05:23 +08:00
2017-09-19 19:38:33 +08:00
ifp = fopen("pieceregular1024.txt", "r");
2017-08-07 21:05:23 +08:00
i = 0;
if (!ifp) {
printf("Cannot Open File");
exit(100);
}
while (!feof(ifp)) {
fscanf(ifp, "%lf \n", &temp[i]);
i++;
}
fclose(ifp);
N = i;
J = 4;
2017-08-07 21:05:23 +08:00
2017-09-19 19:38:33 +08:00
sig = (double*)malloc(sizeof(double)* N);
2017-08-07 21:05:23 +08:00
inp = (double*)malloc(sizeof(double)* N);
oup = (double*)malloc(sizeof(double)* N);
for(i = 0; i < N;++i) {
2017-09-19 19:38:33 +08:00
sig[i] = temp[i];
}
ifp = fopen("PieceRegular10.txt", "r");
i = 0;
if (!ifp) {
printf("Cannot Open File");
exit(100);
}
while (!feof(ifp)) {
fscanf(ifp, "%lf \n", &temp[i]);
i++;
2017-08-07 21:05:23 +08:00
}
2017-09-19 19:38:33 +08:00
fclose(ifp);
for(i = 0; i < N;++i) {
inp[i] = temp[i];
}
2017-09-24 20:54:45 +08:00
obj = denoise_init(N,J,wname);
setDenoiseMethod(obj,"visushrink");// sureshrink is also the default. The other option with dwt and swt is visushrink.
// modwt works only with modwtshrink method
setDenoiseWTMethod(obj,method);// Default is dwt. the other options are swt and modwt
2017-09-24 20:54:45 +08:00
setDenoiseWTExtension(obj,ext);// Default is sym. the other option is per
setDenoiseParameters(obj,thresh,level);// Default for thresh is soft. Other option is hard
// Default for level is all. The other option is first
2017-09-24 20:54:45 +08:00
denoise(obj,inp,oup);
// Alternative to denoise_object
// Just use visushrink, modwtshrink and sureshrink functions
2017-09-24 20:54:45 +08:00
//visushrink(inp,N,J,wname,method,ext,thresh,level,oup);
//sureshrink(inp,N,J,wname,method,ext,thresh,level,oup);
// modwtshrink(sig,N,J,wname,cmethod,ext,thresh,oup); See modwtdenoisetest.c
2017-09-19 19:38:33 +08:00
//ofp = fopen("denoiseds.txt", "w");
2017-08-07 21:05:23 +08:00
2017-09-25 18:06:26 +08:00
printf("Signal - Noisy Signal Stats \n");
2017-09-19 19:38:33 +08:00
printf("RMSE %g\n",rmse(N,sig,inp));
printf("Corr Coeff %g\n",corrcoef(N,sig,inp));
2017-09-25 18:06:26 +08:00
printf("Signal - DeNoised Signal Stats \n");
2017-09-19 19:38:33 +08:00
printf("RMSE %g\n",rmse(N,sig,oup));
printf("Corr Coeff %g\n",corrcoef(N,sig,oup));
2017-08-07 21:05:23 +08:00
2017-09-19 19:38:33 +08:00
free(sig);
2017-08-07 21:05:23 +08:00
free(inp);
2017-09-24 20:54:45 +08:00
denoise_free(obj);
2017-09-25 18:22:42 +08:00
free(oup);
2017-08-07 09:10:11 +08:00
return 0;
}