Ejemplo n.º 1
0
int dtw(int * t1, int * t2, int n, int m) {
	int i;
	int j;
	int window = (int)(0.1*(max(m, n))+0.5); //band size of 10% max of m or n
	int constraint = abs(n-m);
	window = max(window, constraint);

	// create cost matrix
	//int cost[m][n];
	volatile alt_u32 * pCost = ((alt_u32*) SRAM_0_BASE) + sizeof(int)*800;
	int index = 0;
	// setup initial state of matrix
	for (i = 0; i < m; i++){
		for (j = 0; j < n; j++){
			*(pCost +i*n+j) = INFIN;
			//cost[i][j]
			//printf("%d\n", index++);
		}
	}
	//cost[0][0] = 0;
	*pCost = 0;
	// fill matrix
	for (i = 1; i < m; i++) {
		for (j = max(1, i - window); j < min(n, i + window); j++) {
			*(pCost +i*n+j) = min_three(*(pCost +(i-1)*n+j), *(pCost +i*n+(j-1)),
					*(pCost +(i-1)*n+(j-1))) + dist(t1[i], t2[j]);
		}
	}
	return *(pCost + (m-1)*n+(n-1));
	//return pCost[m - 1][n - 1];

}
Ejemplo n.º 2
0
	int minDistance(string word1, string word2) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        // c[i][j] means word1.substr(0, i) and word2.substr(0, j)'s min distance
        int word1_length = word1.size();
        int word2_length = word2.size();
        
        if(word1_length == 0)
            return word2_length;
        if(word2_length == 0)
            return word1_length;
        
        int c[word1_length + 1][word2_length + 1];
        int min_dist = word1_length > word2_length ? word1_length + 1 : word2_length + 1;
        
        //initialize the border, i.e. the edit distance of empty string 
        //and word whose length is x
        for(int i = 0; i < word1_length + 1; ++ i)
        {
            c[i][0] = i;
        }
        
        for(int j = 0; j < word2_length + 1; ++ j)
        {
            c[0][j] = j;
        }
        
        for(int i = 1; i < word1_length + 1; ++ i)
        {
            for(int j = 1; j < word2_length + 1; ++ j)
            {
                if(word1[i - 1] == word2[j - 1])
                {
                    c[i][j] = c[i - 1][j - 1];
                }
                else
                {
                    int num1 = c[i - 1][j];
                    int num2 = c[i - 1][j - 1];
                    int num3 = c[i][j - 1];
                    
                    c[i][j] = min_three(num1, num2, num3) + 1;
                }
            }
        }
        
        return c[word1_length][word2_length];
    }
Ejemplo n.º 3
0
long levenshtein_distance(VALUE str1, VALUE str2) {
	long i, j, s1_len, s2_len, *d;
	char * s = RSTRING_PTR(str1);
	char * t = RSTRING_PTR(str2);
	s1_len = RSTRING_LEN(str1);
	s2_len = RSTRING_LEN(str2);
	
	if (s1_len == 0) {
		return s2_len;
	} else if (s2_len == 0) {
		return s1_len;
	}
	
	// We need one extra col and row for the matrix for starting values
	s1_len++;
	s2_len++;
	
	d = xmalloc(sizeof(typeof(d)) * s1_len * s2_len);
	
	for (i = 0; i < s1_len; i++) {
		d[i] = i; // d[i, 0] = i
	}
	for (j = 0; j < s2_len; j++) {
		d[j*s1_len] = j; // d[0, j] = j
	}
	
	for (i = 1; i < s1_len; i++) {
		for (j = 1; j < s2_len; j++) {
			if (s[i-1] == t[j-1]) {
				d[j * s1_len + i] = d[(j-1) * s1_len + (i-1)];
			} else {
				d[j * s1_len + i] = 1 + min_three(
					d[j * s1_len + (i-1)],
					d[(j-1) * s1_len + i],
					d[(j-1) * s1_len + (i-1)]
				);
			}
		}
	}
	i = d[s1_len * s2_len -1];
	xfree(d);
	return i;
}
Ejemplo n.º 4
0
unsigned long dtw(long * t1, long * t2, int m, int n, int coord) {
	int i;
	int j;
	long zero = 0;
	volatile alt_u32 * pCost;
	int window = (int)(0.1*(max(m, n))+0.5); //band size of 10% max of m or n
	int constraint = abs(n-m);
	window = max(window, constraint);

	// create cost matrix
	//int cost[m][n];
	if(coord == 0){
		pCost = ((alt_u32*) SRAM_0_BASE);// + sizeof(int)*800;
	}
	else{
		pCost = ((alt_u32*) SRAM_0_BASE) + DTW_BASE_Y;
	}
	//long index = 0;
	// setup initial state of matrix
	for (i = 0; i < m; i++){
		for (j = 0; j < n; j++){
			*(pCost +(i*n+j)) = (signed long)INFIN;
			//cost[i][j]
			//printf("%d\n", index++);
		}
	}
	//cost[0][0] = 0;
	*pCost = zero;
	// fill matrix
	for (i = 1; i < m; i++) {
		for (j = max(1, i - window); j < min(n, i + window); j++) {
			*(pCost +(i*n+j)) = min_three((unsigned long)(*(pCost + ((i-1)*n+j))),
					(unsigned long)(*(pCost +(i*n+(j-1)))),
					(unsigned long)(*(pCost +((i-1)*n+(j-1))))) + dist((signed long)*(t1+i), (signed long)*(t2+j));
		}
	}
	return (unsigned long)*(pCost + ((m-1)*n+(n-1))*sizeof(long));
	//return pCost[m - 1][n - 1];

}
Ejemplo n.º 5
0
	// can't pass the large data test
    int minDistance(string word1, string word2) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(word1.size() == 0 && word2.size() == 0)
            return 0;
        if(word1.size() == 0)
            return word2.size();
        if(word2.size() == 0)
            return word1.size();
        
        if(word1[0] == word2[0])
            return minDistance(word1.substr(1), word2.substr(1));
        else
        {
            //delete from word2
            int num1 = minDistance(word1, word2.substr(1));
            //change a single char
            int num2 = minDistance(word1.substr(1), word2.substr(1));
            //delete from word1
            int num3 = minDistance(word1.substr(1), word2);
            
            return 1 + min_three(num1, num2, num3);
        }
    }