This commit is contained in:
2025-03-18 16:28:43 +08:00
parent e7134f0f81
commit 47cbe478eb
16 changed files with 179 additions and 912 deletions

5
examples/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
*.pyc
eemd_example
ceemdan_example
eemd_example.out
ceemdan_example.out

7
examples/CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
macro(add_example src)
add_executable(${src} ${src}.cpp)
target_link_libraries(${src} PUBLIC eemd)
endmacro()
add_example(eemd_example)
add_example(ceemdan_example)

4
examples/README Normal file
View File

@@ -0,0 +1,4 @@
`eemd_example` is a simple example program for the C interface of libeemd. It
writes its output to `eemd_example.out`. If you have installed `pyeemd` you can
plot the results with the Python script `eemd_example_plot.py`. To compile the
example please run `make`.

View File

@@ -0,0 +1,68 @@
/* Copyright 2013 Perttu Luukko
* This file is part of libeemd.
* libeemd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* libeemd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with libeemd. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_math.h>
const double pi = M_PI;
#include "../src/eemd.h"
const size_t ensemble_size = 500;
const unsigned int S_number = 4;
const unsigned int num_siftings = 50;
const double noise_strength = 0.02;
const unsigned long int rng_seed = 0;
const char outfile[] = "ceemdan_example.out";
const size_t N = 512;
int main(void) {
libeemd_error_code err;
// As an example decompose a Dirac signal as in the original CEEMDAN paper
double* inp = (double*) malloc(N*sizeof(double));
memset(inp, 0x00, N*sizeof(double));
inp[N/2] = 1.0;
// Allocate memory for output data
size_t M = emd_num_imfs(N);
double* outp = (double*) malloc(M*N*sizeof(double));
// Run CEEMDAN
err = ceemdan(inp, N, outp, M, ensemble_size, noise_strength, S_number, num_siftings, rng_seed);
if (err != EMD_SUCCESS) {
emd_report_if_error(err);
exit(1);
}
// Write output to file
FILE* fp = fopen(outfile, "w");
for (size_t j=0; j<N; j++) {
fprintf(fp, "%f ", inp[j]);
}
fprintf(fp, "\n");
for (size_t i=0; i<M; i++) {
for (size_t j=0; j<N; j++) {
fprintf(fp, "%f ", outp[i*N+j]);
}
fprintf(fp, "\n");
}
printf("Done!\n");
// Cleanup
fclose(fp);
free(inp); inp = NULL;
free(outp); outp = NULL;
}

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env python2
# Copyright 2013 Perttu Luukko
# This file is part of libeemd.
#
# libeemd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libeemd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libeemd. If not, see <http://www.gnu.org/licenses/>.
from numpy import loadtxt
from pylab import plot, show
from pyeemd.utils import plot_imfs
def main():
data = loadtxt("ceemdan_example.out")
orig = data[0]
imfs = data[1:]
plot(orig, label="Original data")
plot_imfs(imfs)
show()
if __name__ == "__main__":
main()

74
examples/eemd_example.cpp Normal file
View File

@@ -0,0 +1,74 @@
/* Copyright 2013 Perttu Luukko
* This file is part of libeemd.
* libeemd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* libeemd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with libeemd. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_math.h>
const double pi = M_PI;
#include "../src/eemd.h"
const size_t ensemble_size = 250;
const unsigned int S_number = 4;
const unsigned int num_siftings = 50;
const double noise_strength = 0.2;
const unsigned long int rng_seed = 0;
const char outfile[] = "eemd_example.out";
// An example signal to decompose
const size_t N = 1024;
static inline double input_signal(double x) {
const double omega = 2*pi/(N-1);
return sin(17*omega*x)+0.5*(1.0-exp(-0.002*x))*sin(51*omega*x+1);
}
int main(void) {
libeemd_error_code err;
// Define input data
double* inp = (double*) malloc(N*sizeof(double));
for (size_t i=0; i<N; i++) {
inp[i] = input_signal((double)i);
}
// Allocate memory for output data
size_t M = emd_num_imfs(N);
double* outp = (double*) malloc(M*N*sizeof(double));
// Run eemd
err = eemd(inp, N, outp, M, ensemble_size, noise_strength, S_number, num_siftings, rng_seed);
if (err != EMD_SUCCESS) {
emd_report_if_error(err);
exit(1);
}
// Write output to file
FILE* fp = fopen(outfile, "w");
for (size_t j=0; j<N; j++) {
fprintf(fp, "%f ", inp[j]);
}
fprintf(fp, "\n");
for (size_t i=0; i<M; i++) {
for (size_t j=0; j<N; j++) {
fprintf(fp, "%f ", outp[i*N+j]);
}
fprintf(fp, "\n");
}
printf("Done!\n");
// Cleanup
fclose(fp);
free(inp); inp = NULL;
free(outp); outp = NULL;
}

33
examples/eemd_example_plot.py Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env python2
# Copyright 2013 Perttu Luukko
# This file is part of libeemd.
#
# libeemd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libeemd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libeemd. If not, see <http://www.gnu.org/licenses/>.
from numpy import loadtxt
from pylab import plot, show
from pyeemd.utils import plot_imfs
def main():
data = loadtxt("eemd_example.out")
orig = data[0]
imfs = data[1:]
plot(orig, label="Original data")
plot_imfs(imfs)
show()
if __name__ == "__main__":
main()