Skip to content

Commit e6771b7

Browse files
committed
remove the c_
1 parent b36afe6 commit e6771b7

File tree

5 files changed

+55
-54
lines changed

5 files changed

+55
-54
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
c_iir_test
1+
iir_test
22
*~
33
*.o
44
*.swp

Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ CFLAGS+=-Wall -Wextra -Wpedantic
22
LDFLAGS+=-DFLOAT_TYPE=double
33

44
.PHONY: all test clean
5-
all: c_iir_test
5+
all: iir_test
66

7-
c_iir_test: c_iir_test.c c_iir.c
7+
iir_test: iir_test.c iir.c
88
cc $(CFLAGS) -o $@ $^ $(LDFLAGS)
99

10-
test: c_iir_test
11-
./c_iir_test
10+
test: iir_test
11+
./iir_test
1212

1313
clean:
14-
rm -rf *.o *~ c_iir_test
14+
rm -rf *.o *~ iir_test

c_iir.c iir.c

+19-9
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424

25-
#include "c_iir.h"
25+
#include "iir.h"
2626
#include <string.h>
2727
#include <stdio.h>
2828

29-
FLOAT_TYPE iir_calculate( const struct c_iir * iir, FLOAT_TYPE input)
29+
FLOAT_TYPE iir_calculate( const struct iir * iir, FLOAT_TYPE input)
3030
{
3131
unsigned int i;
3232
unsigned int num_size = iir->num_size;
@@ -49,34 +49,44 @@ FLOAT_TYPE iir_calculate( const struct c_iir * iir, FLOAT_TYPE input)
4949
return result;
5050
}
5151

52-
void iir_to_general_form( struct c_iir * iir )
52+
int iir_check( struct iir * iir )
53+
{
54+
unsigned int num_size = iir->num_size;
55+
unsigned int den_size = iir->den_size;
56+
if ( num_size == 0 ) return -1; //cannot have a zero size numerator
57+
if ( den_size < num_size ) return -2; //requirement of 'standard programming'
58+
return 0;
59+
60+
}
61+
62+
void iir_to_general_form( struct iir * iir )
5363
{
5464
unsigned int num_size = iir->num_size;
5565
unsigned int den_size = iir->den_size;
5666
FLOAT_TYPE * num = iir->num;
5767
FLOAT_TYPE * den = iir->den;
58-
FLOAT_TYPE val = den[0];
68+
FLOAT_TYPE first = den[0];
5969

60-
if (val != 1.0)
70+
if (first != 1.0)
6171
{
6272
unsigned int i;
6373
for (i = 0; i < num_size; i++)
6474
{
65-
num[i] /= val;
75+
num[i] /= first;
6676
}
6777
for (i = 0; i < den_size; i++)
6878
{
69-
den[i] /= val;
79+
den[i] /= first;
7080
}
7181
}
7282
}
7383

74-
void iir_init_zero( struct c_iir * iir )
84+
void iir_init_zero( struct iir * iir )
7585
{
7686
memset( iir->history, 0, sizeof(FLOAT_TYPE) * iir->den_size); \
7787
}
7888

79-
FLOAT_TYPE iir_dc_gain( const struct c_iir * iir )
89+
FLOAT_TYPE iir_dc_gain( const struct iir * iir )
8090
{
8191
unsigned int i;
8292
unsigned int num_size = iir->num_size;

c_iir.h iir.h

+28-37
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,34 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424

25-
#ifndef _C_IIR_H_
26-
#define _C_IIR_H_
25+
/*
26+
Implement the "Standard Programming" approach as described
27+
in "Discrete-Time Control Systems" by Katsuhiko Ogata for
28+
the general form of the transfer function:
29+
Output(z) b[0] + b[1]*z^-1 + b[2]*z^-2 + ... + b[m]*z^-m
30+
--------- = ------------------------------------------------
31+
Input(z) a[0] + a[1]*z^-1 + a[2]*z^-2 + ... + a[n]*z^-n
32+
33+
The "Standard Programming" approach requires:
34+
n >= m
35+
a[0] == 1
36+
size(history) == n
37+
*/
38+
39+
#ifndef _IIR_H_
40+
#define _IIR_H_
2741

2842
#ifndef FLOAT_TYPE
2943
#error FLOAT_TYPE must be specified
3044
#endif
3145

32-
//#include <assert.h>
33-
3446
#define IIR_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
3547
#define IIR_ARRAY(...) {__VA_ARGS__}
3648
#define IIR_INIT(name, num, den) \
3749
FLOAT_TYPE name##_num[] = num; \
3850
FLOAT_TYPE name##_den[] = den; \
3951
FLOAT_TYPE name##_history[] = den; \
40-
struct c_iir name = \
52+
struct iir name = \
4153
{ \
4254
IIR_SIZE(name##_num), \
4355
IIR_SIZE(name##_den), \
@@ -46,22 +58,8 @@ struct c_iir name = \
4658
name##_history \
4759
}; \
4860
iir_init_zero( &name )
49-
//assert( name.num_size > 0 && "cannot have a zero size numerator" );
50-
//assert( name.den_size > 0 && "cannot have a zero size denominator" );
51-
//assert( name.den_size >= name.num_size && "requirement of 'standard programming'" )
52-
53-
/*
54-
struct c_iir represents a general discrete time transfer function such as:
5561

56-
Output(z) b[0] + b[1]*z^-1 + b[2]*z^-2 + ... + b[m]*z^-m
57-
--------- = ------------------------------------------------
58-
Input(z) a[0] + a[1]*z^-1 + a[2]*z^-2 + ... + a[n]*z^-n
59-
60-
Where:
61-
b is an array of length m that represents the numerator coefficients.
62-
a is an array of length n that represents the denominator coefficients.
63-
*/
64-
struct c_iir
62+
struct iir
6563
{
6664
unsigned int num_size; // number of elements in num
6765
unsigned int den_size; // number of elements in den
@@ -70,26 +68,19 @@ struct c_iir
7068
FLOAT_TYPE * history; // pointer to array of size den_size
7169
};
7270

73-
/*
74-
IIR_calculate implements the "Standard Programming" approach as described
75-
in "Discrete-Time Control Systems" by Katsuhiko Ogata.
71+
//calculate the transfer function output for the current step
72+
FLOAT_TYPE iir_calculate( const struct iir * iir, FLOAT_TYPE input );
7673

77-
The "Standard Programming" approach requires:
78-
n >= m
79-
a[0] == 1
80-
size(history) == n
81-
*/
82-
FLOAT_TYPE iir_calculate( const struct c_iir * iir, FLOAT_TYPE input );
74+
//return 0 if some basic requirements are met
75+
int iir_check( struct iir * iir );
8376

84-
/*
85-
check if iir is in the general form and updates it if possible.
86-
*/
87-
void iir_to_general_form( struct c_iir * iir );
77+
//check if iir is in the general form and updates it if possible.
78+
void iir_to_general_form( struct iir * iir );
8879

89-
/**/
90-
void iir_init_zero( struct c_iir * iir );
80+
//zero out the history
81+
void iir_init_zero( struct iir * iir );
9182

92-
/**/
93-
FLOAT_TYPE iir_dc_gain( const struct c_iir * iir );
83+
//compute the "dc gain" of the transfer function
84+
FLOAT_TYPE iir_dc_gain( const struct iir * iir );
9485

9586
#endif

c_iir_test.c iir_test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424

25-
#include "c_iir.h"
25+
#include "iir.h"
2626
#include <stdio.h>
2727

2828
#define EPS 1.0E-14
@@ -31,7 +31,7 @@ SOFTWARE.
3131
#define BACK(i) "\x1B[48;5;" #i "m"
3232
#define RESET "\x1b[0m"
3333

34-
void run_test( FLOAT_TYPE * input, int in_count, FLOAT_TYPE * expected, int expected_count, const struct c_iir * iir, const char name[] )
34+
void run_test( FLOAT_TYPE * input, int in_count, FLOAT_TYPE * expected, int expected_count, const struct iir * iir, const char name[] )
3535
{
3636
printf("TEST %s:\n", name );
3737
FLOAT_TYPE output, error;

0 commit comments

Comments
 (0)