Skip to content

Commit da3d87c

Browse files
committed
Initial commit of files from thesis
1 parent 677f187 commit da3d87c

File tree

95 files changed

+5568
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+5568
-0
lines changed

CRMS.c

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Computes the windowed RMS of a signal
3+
*
4+
* INPUTS:
5+
* padded_signal: [double column vector] the padded signal
6+
* sig_length : [int] the original signal length
7+
* pad_size : [int] the padding size used to pad the signal before computing the RMS
8+
* window_length: [int] the length of the RMS window to use
9+
*
10+
* Sagi Perel, 05/2008
11+
*/
12+
13+
#include "mex.h"
14+
#include <string.h>
15+
#include <math.h>
16+
17+
double array_mean(double* arr, int arr_size)
18+
{
19+
int i;
20+
double mean=arr[0];
21+
for(i=1; i<arr_size; i++)
22+
mean += arr[i];
23+
24+
return mean/arr_size;
25+
}
26+
27+
void array_pow2(double* arr, int arr_size)
28+
{
29+
int i;
30+
for(i=0; i<arr_size; i++)
31+
arr[i] = pow(arr[i], 2);
32+
}
33+
34+
/*
35+
* Assumes that sig_rms is already allocated and has the size of sig_len
36+
*/
37+
void RMS(double* padded_sig, int sig_len, int padded_sig_length, int pad_size, double* sig_rms)
38+
{
39+
int i;
40+
double sample_rms_val = 0;
41+
int window_len = pad_size*2+1; //length of the running window we will use
42+
#ifdef _WIN_CMRS
43+
//the MS compiler doesn't like it - so let Matlab do allocation for us
44+
double *tmp;
45+
mxArray *tmpmxArray;
46+
tmpmxArray = mxCreateDoubleMatrix(window_len, 1, mxREAL);
47+
tmp = mxGetPr(tmpmxArray);
48+
#else
49+
double tmp[window_len];
50+
#endif
51+
52+
//compute the RMS
53+
for(i=pad_size; i<sig_len+pad_size; i++)
54+
{
55+
//to compute an RMS sample- we need to work on a small window
56+
memcpy(tmp, &padded_sig[i-pad_size], sizeof(double)*window_len);
57+
array_pow2(tmp,window_len);
58+
//compute the sqrt of the mean
59+
sample_rms_val = sqrt(array_mean(tmp,window_len));
60+
sig_rms[i-pad_size] = sample_rms_val;
61+
}
62+
#ifdef _WIN_CMRS
63+
mxDestroyArray(tmpmxArray);
64+
#endif
65+
}
66+
67+
void
68+
mexFunction(
69+
int num_output_args, // Number of left hand side (output) arguments
70+
mxArray *output_arg[], // Array of left hand side arguments
71+
int num_input_args, // Number of right hand side (input) arguments
72+
const mxArray *input_arg[]) // Array of right hand side arguments
73+
{
74+
double *padded_sig, *sig_rms, *tmp;
75+
int sig_length, padded_sig_length, pad_size, window_length;
76+
77+
//sanity check for input arguments
78+
if( num_input_args != 4) mexErrMsgTxt( "RMS.c: wrong syntax: You should use RMS(padded_signal, original_signal_length, pad_size, window_length)\n");
79+
if( mxGetN(input_arg[0]) != 1) mexErrMsgTxt( "RMS.c: the signal must be a column vector\n");
80+
81+
//read input arguments:
82+
//-read the signal and its size
83+
padded_sig = (double *) mxGetData( input_arg[0]);
84+
padded_sig_length = mxGetM(input_arg[0]);
85+
86+
tmp = (double *) mxGetData( input_arg[1]);
87+
sig_length = (int)(*tmp);
88+
89+
//-read the pad size
90+
tmp = (double *) mxGetData( input_arg[2]);
91+
pad_size = (int) (*tmp);
92+
93+
//-read the window length
94+
tmp = (double *) mxGetData( input_arg[3]);
95+
window_length = (int) (*tmp);
96+
97+
//create an output array
98+
output_arg[0] = mxCreateDoubleMatrix(sig_length, 1, mxREAL);
99+
sig_rms = mxGetPr(output_arg[0]);
100+
101+
RMS(padded_sig, sig_length, padded_sig_length, pad_size, sig_rms);
102+
}

CRMS.mexa64

11.5 KB
Binary file not shown.

CRMS.mexw32

7.5 KB
Binary file not shown.

Cfind_first_bigger_sample_in_array2.c

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Finds the first bigger sample in the given array
3+
*
4+
* INPUTS:
5+
* array : [double/single column vector] a sorted array
6+
* value : [double/single]
7+
*
8+
* OUTPUT:
9+
* index : [double] the
10+
*
11+
* Sagi Perel, 10/2009
12+
*/
13+
14+
#include "mex.h"
15+
#include "matrix.h"
16+
17+
void
18+
mexFunction(
19+
int num_output_args, // Number of left hand side (output) arguments
20+
mxArray *output_arg[], // Array of left hand side arguments
21+
int num_input_args, // Number of right hand side (input) arguments
22+
const mxArray *input_arg[]) // Array of right hand side arguments
23+
{
24+
double *array, value, *ptr;
25+
float *array_f;
26+
int array_length, this_array_length;
27+
mxClassID array_type;
28+
int debug=0;
29+
int i;
30+
int start_idx, end_idx, middle_idx;
31+
double epsilon = 0.00001;
32+
33+
//sanity check for input arguments
34+
if( num_input_args != 2) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: wrong syntax: Cfind_first_bigger_sample_in_array(array, value)\n");
35+
if( mxGetN(input_arg[0]) != 1 && mxGetM(input_arg[0]) != 1 ) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the array must be a vector\n");
36+
array_type = mxGetClassID(input_arg[0]);
37+
if( array_type != mxDOUBLE_CLASS && array_type != mxSINGLE_CLASS ) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the array must be a column vector of type double or float\n");
38+
39+
if( mxGetN(input_arg[1]) != 1) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the value must be a double\n");
40+
if( mxGetM(input_arg[1]) != 1) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the value must be a double\n");
41+
42+
array_length = (mxGetM(input_arg[0]) > mxGetN(input_arg[0])) ? mxGetM(input_arg[0]) : mxGetN(input_arg[0]);
43+
44+
//read input arguments:
45+
ptr = (double*) mxGetData( input_arg[1]);
46+
value = *ptr;
47+
if(debug)
48+
printf("array_length=%d,value=%f\n",array_length,value);
49+
50+
51+
//-read the signal and find the first
52+
switch(array_type)
53+
{
54+
case mxDOUBLE_CLASS:
55+
array = (double *) mxGetData( input_arg[0]);
56+
57+
//check for edge condition- if the first element is bigger than the value
58+
if((array[0] - value)>0)
59+
{
60+
output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
61+
*mxGetPr(output_arg[0]) = 1;
62+
return;
63+
}
64+
65+
//shrink the array size by half every time
66+
start_idx = 0;
67+
end_idx = array_length-1;
68+
while(1)
69+
{
70+
71+
this_array_length = end_idx - start_idx + 1;
72+
if(debug)
73+
printf("start_idx=%d end_idx=%d (%d)",start_idx, end_idx, this_array_length);
74+
75+
if(this_array_length <= 20)
76+
{
77+
middle_idx = search_array_serial(array, value, start_idx, this_array_length);
78+
if(debug)
79+
printf("\nthis_array_length<=20: middle_idx=%d\n",middle_idx);
80+
if(middle_idx < 0)
81+
{
82+
output_arg[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
83+
return;
84+
}else{
85+
86+
output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
87+
*mxGetPr(output_arg[0]) = middle_idx;
88+
return;
89+
}
90+
}
91+
92+
middle_idx = (this_array_length % 2 == 0 )? (start_idx+(this_array_length/2)) : (start_idx+((this_array_length-1)/2));
93+
if(debug)
94+
printf(" middle_idx=%d,val=%f\n",middle_idx,array[middle_idx]);
95+
//shrink the array size by half
96+
if(mxIsNaN(array[middle_idx]))
97+
{
98+
if(middle_idx == start_idx)
99+
{
100+
output_arg[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
101+
return;
102+
}else{
103+
middle_idx = middle_idx -1;
104+
}
105+
}else{
106+
/*
107+
//if the middle_idx is exactly the value we are looking for- numerical issues might cause to miss it
108+
if(array[middle_idx]-epsilon <= value && array[middle_idx]+epsilon >= value)
109+
{
110+
output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
111+
*mxGetPr(output_arg[0]) = middle_idx+1;
112+
return;
113+
}
114+
*/
115+
116+
//else continue as usual
117+
if((array[middle_idx] - value) > 0)
118+
{
119+
end_idx = middle_idx;
120+
}else{
121+
start_idx = middle_idx;
122+
}
123+
}
124+
}
125+
126+
break;
127+
case mxSINGLE_CLASS:
128+
array_f = (float *) mxGetData( input_arg[0]);
129+
for(i=0; i<array_length; i++)
130+
{
131+
if((array_f[i] - value) > 0)
132+
{
133+
output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
134+
*mxGetPr(output_arg[0]) = (i+1);
135+
return;
136+
}
137+
}
138+
break;
139+
}
140+
141+
//create an empty output, to be returned if no such item exists
142+
output_arg[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
143+
}
144+
145+
146+
int search_array_serial(double* array, double value, int start_idx, int array_length)
147+
{
148+
int i;
149+
for(i=start_idx; i<start_idx+array_length; i++)
150+
{
151+
if((array[i] - value) >= 0)
152+
{
153+
return (i+1);
154+
}
155+
}
156+
return -1;
157+
}
158+
12.3 KB
Binary file not shown.
7.5 KB
Binary file not shown.
8 KB
Binary file not shown.

0 commit comments

Comments
 (0)