1 /*--------------------------------------------------------------
2 	Signal processing function library    libsgp
3 	  ( Correlation Functions Rev.941214)
4 		Written by H.Goto , Dec.1994
5 --------------------------------------------------------------*/
6 
7 /*--------------------------------------------------------------------
8   Copyright (C) 1994  Hideaki Goto
9 
10         All Rights Reserved
11 
12   Permission to use, copy, modify, and distribute this software and
13   its documentation for any purpose is hereby granted without fee,
14   provided that (i) the above copyright notice and this permission
15   notice appear in all copies and in supporting documentation, (ii)
16   the name of the author, Hideaki Goto, may not be used in any
17   advertising or otherwise to promote the sale, use or other
18   dealings in this software without prior written authorization
19   from the author, (iii) this software may not be used for
20   commercial products without prior written permission from the
21   author, and (iv) the notice of the modification is specified in
22   case of that the modified copies of this software are distributed.
23 
24   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
25   THE AUTHOR WILL NOT BE RESPONSIBLE FOR ANY DAMAGE CAUSED BY THIS
26   SOFTWARE.
27 --------------------------------------------------------------------*/
28 
29 
30 #include	<stdio.h>
31 #include	<memory.h>
32 
33 #include	"sgplib.h"
34 
35 
36 /*------------------------------------------------------
37 	Get Correlation factor
38 ------------------------------------------------------*/
39 
sgp_correlations(short * d1,short * d2,int len,short * corr,int maxmove,int periodic,short zero)40 int sgp_correlations(short *d1,short *d2,int len,short *corr, \
41 				int maxmove,int periodic,short zero){
42 	int	i,mv;
43 	short	sum;
44 	for ( mv=0 ; mv<maxmove ; mv++ ){
45 		sum = 0;
46 		for ( i=0 ; i < (len - mv) ; i++ ){
47 			sum += d1[i] * d2[i + mv];
48 		}
49 		for ( i = (len - mv) ; i<len ; i++ ){
50 			if ( periodic )  sum += d1[i] * d2[i + mv - len];
51 			else             sum += d1[i] * zero;
52 		}
53 		*corr++ = sum;
54 	}
55 	return(0);
56 }
57 
58 
sgp_correlationi(int * d1,int * d2,int len,int * corr,int maxmove,int periodic,int zero)59 int sgp_correlationi(int *d1,int *d2,int len,int *corr, \
60 				int maxmove,int periodic,int zero){
61 	int	i,mv;
62 	int	sum;
63 	for ( mv=0 ; mv<maxmove ; mv++ ){
64 		sum = 0;
65 		for ( i=0 ; i < (len - mv) ; i++ ){
66 			sum += d1[i] * d2[i + mv];
67 		}
68 		if ( periodic ){
69 			for ( i = (len - mv) ; i<len ; i++ )
70 				sum += d1[i] * d2[i + mv - len];
71 		}
72 		else{	for ( i = (len - mv) ; i<len ; i++ )
73 				sum += d1[i] * zero;
74 		}
75 		*corr++ = sum;
76 	}
77 	return(0);
78 }
79 
80 
sgp_correlationb(char * d1,char * d2,int len,int * corr,int maxmove,int periodic,char zero)81 int sgp_correlationb(char *d1,char *d2,int len,int *corr, \
82 				int maxmove,int periodic,char zero){
83 	int	i,mv;
84 	int	sum;
85 	for ( mv=0 ; mv<maxmove ; mv++ ){
86 		sum = 0;
87 		for ( i=0 ; i < (len - mv) ; i++ ){
88 			if ( ! (d1[i] ^ d2[i + mv]) )  ++sum;
89 		}
90 		if ( periodic ){
91 			for ( i = (len - mv) ; i<len ; i++ )
92 				if ( ! (d1[i] ^ d2[i + mv - len]) )  ++sum;
93 		}
94 		else{	for ( i = (len - mv) ; i<len ; i++ )
95 				if ( ! (d1[i] ^ (char)zero) )  ++sum;
96 		}
97 		*corr++ = (2 * sum) -len;
98 	}
99 	return(0);
100 }
101 
102 
103