1 /*--------------------------------------------------------------
2 	Signal processing function library    libsgp
3 	  ( Walsh transform functions  Rev.020527 )
4 		Written  by H.Goto , May 1993
5 		Modified by H.Goto , May 2002
6 --------------------------------------------------------------*/
7 
8 /*--------------------------------------------------------------------
9   Copyright (C) 1993-2002  Hideaki Goto
10 
11         All Rights Reserved
12 
13   Permission to use, copy, modify, and distribute this software and
14   its documentation for any purpose is hereby granted without fee,
15   provided that (i) the above copyright notice and this permission
16   notice appear in all copies and in supporting documentation, (ii)
17   the name of the author, Hideaki Goto, may not be used in any
18   advertising or otherwise to promote the sale, use or other
19   dealings in this software without prior written authorization
20   from the author, (iii) this software may not be used for
21   commercial products without prior written permission from the
22   author, and (iv) the notice of modification is specified in cases
23   where modified copies of this software are distributed.
24 
25   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
26   THE AUTHOR WILL NOT BE RESPONSIBLE FOR ANY DAMAGE CAUSED BY THIS
27   SOFTWARE.
28 --------------------------------------------------------------------*/
29 
30 
31 #include	<stdio.h>
32 #include	<stdlib.h>
33 #include	<memory.h>
34 
35 #include	"sgplib.h"
36 
37 
38 
39 
40 /*------------------------------------------------------
41 	Get Walsh sequences
42 ------------------------------------------------------*/
43 
sgp_wal(int n,int m,int nn)44 int sgp_wal(int n,int m,int nn){
45 	if ( m == 0 )  return(1);
46 	n = n % nn;
47 	if ( m == 1 ){
48 		if ( n < (nn/2) )  return(1);  else  return(-1);
49 	}
50 	if ( m & 0x02 )
51 		return ( sgp_wal(2 * n, (m/2), nn) * sgp_wal(n, 1-(m & 0x01), nn) );
52 	return ( sgp_wal(2 * n, (m/2), nn) * sgp_wal(n, m & 0x01, nn) );
53 
54 }
55 
56 
sgp_walseq(int * buf,int m,int nn)57 void sgp_walseq(int *buf,int m,int nn){
58 	int	i;
59 	for ( i=0 ; i<nn ; i++ )  *buf++ = sgp_wal(i,m,nn);
60 }
61 
62 
sgp_walseqb(char * buf,int m,int nn)63 void sgp_walseqb(char *buf,int m,int nn){
64 	int	i;
65 	for ( i=0 ; i<nn ; i++ )
66 		if ( sgp_wal(i,m,nn) +1 )  *buf++ = 0xff;  else  *buf++ = 0;
67 }
68 
69 
70 
71 
72 /*------------------------------------------------------
73 	Create Walsh Matrix
74 ------------------------------------------------------*/
75 
sgp_walmatrix(int * buf,int nn)76 void sgp_walmatrix(int *buf,int nn){
77 	int	i;
78 	for ( i=0 ; i<nn ; i++ ){
79 		sgp_walseq(buf,i,nn);
80 		buf += nn;
81 	}
82 }
83 
84 
sgp_walmatrixb(char * buf,int nn)85 void sgp_walmatrixb(char *buf,int nn){
86 	int	i;
87 	for ( i=0 ; i<nn ; i++ ){
88 		sgp_walseqb(buf,i,nn);
89 		buf += nn;
90 	}
91 }
92 
93 
94 
95 
96 /*------------------------------------------------------
97 	Walsh Transform
98 ------------------------------------------------------*/
99 
sgp_WHT(int * data,int * sequency,int len,int * wmtx)100 int sgp_WHT(int *data,int *sequency,int len,int *wmtx){
101 	int	m,n,sum;
102 	int	*wal,*wp;
103 	if ( 0 == (wal = wmtx) ){
104 		if ( 0 == (wal = (int *)malloc(len * len * sizeof(int))) )  return(-1);
105 		sgp_walmatrix(wal,len);
106 	}
107 	for ( m=0 ; m<len ; m++ ){
108 		sum = 0;
109 		wp = &wal[len * m];
110 		for ( n=0 ; n<len ; n++ )  sum += data[n] * (*wp++);
111 		*sequency++ = sum;
112 	}
113 	if ( 0 != wmtx )  free(wal);
114 	return(0);
115 }
116 
117 
sgp_WHTb(char * data,int * sequency,int len,char * wmtx)118 int sgp_WHTb(char *data,int *sequency,int len,char *wmtx){
119 	int	m,n,sum;
120 	char	*wal,*wp;
121 	if ( 0 == (wal = wmtx) ){
122 		if ( 0 == (wal = (char *)malloc(len * len * sizeof(char))) )  return(-1);
123 		sgp_walmatrixb(wal,len);
124 	}
125 	for ( m=0 ; m<len ; m++ ){
126 		sum = 0;
127 		wp = &wal[len * m];
128 		for ( n=0 ; n<len ; n++ )  if ( ! (data[n] ^ (*wp++)) )  ++sum;
129 		*sequency++ = (2 * sum) - len;
130 	}
131 	if ( 0 != wmtx )  free(wal);
132 	return(0);
133 }
134 
135 
sgp_WHTpower(int * wseries,int * pow,int len)136 void sgp_WHTpower(int *wseries,int *pow,int len){
137 	int	i;
138 	*pow++ = *wseries * *wseries;
139 	++wseries;
140 	for ( i=1 ; i<(len/2) ; i++ ){
141 		*pow++ = (wseries[0] * wseries[0]) + (wseries[1] * wseries[1]);
142 		wseries += 2;
143 	}
144 	*pow++ = *wseries * *wseries;
145 }
146 
147 
148