END_TEST

START_TEST(test_vector_mean) {
  u32 i, t;

  seed_rng();
  for (t = 0; t < LINALG_NUM; t++) {
    u32 n = sizerand(MSIZE_MAX);
    double A[n];
    double test = mrand/1e22;
    for (i = 0; i < n; i++)
      A[i] = test + i;
    double mean = vector_mean(n, A);
    double expect = test + (n - 1.0) / 2.0;
    fail_unless(fabs(mean - expect)
                < LINALG_TOL,
                "Mean differs from expected %lf: %lf (%lf)",
                expect, mean, fabs(mean - expect));
  }
}
Exemple #2
0
void separation(double * input_1, double * input_2, double * output_1, double * output_2, int nb, int time_delay) { 

    // Initialize vectors
    Vector* v_s1 = malloc(sizeof(Vector));
    Vector* v_s2 = malloc(sizeof(Vector));
    v_s1->elems = input_1;
    v_s2->elems = input_2;
    v_s1->L = nb;
    v_s2->L = nb;

    // Center mixes
    vector_substract_scalar_inplace(v_s1, vector_mean(v_s1));
    vector_substract_scalar_inplace(v_s2, vector_mean(v_s2));

    // Normalize
    double normalisation = sqrt(2 / (vector_std(v_s1) * vector_std(v_s1) + vector_std(v_s2) * vector_std(v_s2)));
    for (int i = 0; i < v_s1->L; i++) {
        v_s1->elems[i] *= normalisation;
        v_s2->elems[i] *= normalisation;
    }
    
    // Estimate covariance
    int M = time_delay; // Number of time delays for correlation
    int N = v_s1->L / M; // We assume s1 and s2 are the same length

    /// Reshape 1xL to M*(L/M)
    Matrix* m_x1 = reshape(v_s1, M, N);
    Matrix* m_x2 = reshape(v_s2, M, N);
    
    /// Covariance estimation
    Matrix* m_x1t = transpose(m_x1);
    Matrix* m_x2t = transpose(m_x2);

    Matrix* m_r11 = multiply(m_x1, m_x1t);
    matrix_multiply_scalar_inplace(m_r11, (1.0f / N));

    Matrix* m_r12 = multiply(m_x1, m_x2t);
    matrix_multiply_scalar_inplace(m_r12, (1.0f / N));
    
    Matrix* m_r22 = multiply(m_x2, m_x2t);
    matrix_multiply_scalar_inplace(m_r22, (1.0f / N));

    ///Compute coefficients
    double s_f1  = off(m_r11);
    double s_f2  = off(m_r22);
    double s_f12 = off(m_r12);
    double s_t1  = tr(m_r11);
    double s_t2  = tr(m_r22);
    double s_t12 = tr(m_r12);

    double sigma2 = 0;
    double alpha = (2 * s_f12 * s_t12) - (s_f1 * (s_t2 - sigma2) + s_f2 * (s_t1 - sigma2));
    double beta = 2 * (pow(s_t12, 2) - (s_t1 - sigma2) * (s_t2 - sigma2));
    double gamma2 = pow((s_f1*(s_t2 - sigma2) - s_f2*(s_t1 - sigma2)), 2)
                    + 4 * (s_f12*(s_t2 - sigma2) - s_t12*s_f2) * (s_f12*(s_t1 - sigma2) - s_t12*s_f1);

    double s_d1 = alpha - sqrt(gamma2);
    double s_d2 = alpha + sqrt(gamma2);
    
    // Generate matrix
    Matrix* m_est = allocate_matrix(2, 2);

    m_est->elems[0][0] = beta * s_f1  - s_t1  * s_d1;
    m_est->elems[0][1] = beta * s_f12 - s_t12 * s_d2;
    m_est->elems[1][0] = beta * s_f12 - s_t12 * s_d1;
    m_est->elems[1][1] = beta * s_f2  - s_t2  * s_d2;

    // Invert matrix
    double det = (m_est->elems[0][0]* m_est->elems[1][1]) - (m_est->elems[0][1]* m_est->elems[1][0]);

    Matrix* m_est_inv = allocate_matrix(2, 2);
    m_est_inv->elems[0][0] =  m_est->elems[1][1];
    m_est_inv->elems[0][1] = -m_est->elems[0][1];
    m_est_inv->elems[1][0] = -m_est->elems[1][0];
    m_est_inv->elems[1][1] =  m_est->elems[0][0];
    matrix_multiply_scalar_inplace(m_est_inv, 1 / det);

    printf("\nEstimated mixing matrix\n");
    debug_matrix(m_est, 2, 2);
    printf("\nEstimated invert mixing matrix\n");
    debug_matrix(m_est_inv, 2, 2);
    
    det = (m_est_inv->elems[0][0]* m_est_inv->elems[1][1]) - (m_est_inv->elems[0][1]* m_est_inv->elems[1][0]);

    // Sources separation
    for (int i = 0; i < v_s1->L; i++) {
        output_1[i] = (m_est_inv->elems[0][0]*v_s1->elems[i] + m_est_inv->elems[0][1]*v_s2->elems[i]) / det;
        output_2[i] = (m_est_inv->elems[1][0]*v_s1->elems[i] + m_est_inv->elems[1][1]*v_s2->elems[i]) / det;
    }
}
float64 vector_vqgen (float32 **data, int32 rows, int32 cols, int32 vqrows,
		      float64 epsilon, int32 maxiter,
		      float32 **mean, int32 *map)
{
    int32 i, j, r, it;
    static uint32 seed = 1;
    float64 sqerr, prev_sqerr=0, t;
    bitvec_t sel;
    int32 *count;
    float32 *gmean;
    ptmr_t tm;
    
    assert ((rows >= vqrows) && (maxiter >= 0) && (epsilon > 0.0));
    
    sel = bitvec_alloc (rows);
    
    ptmr_init (&tm);
    ptmr_start (&tm);
    
    /* Pick a random initial set of centroids */
#ifndef WIN32			/* RAH */
    srandom (seed);
    seed ^= random();
#else  /* RAH */
      srand ((unsigned) time(NULL)); /* RAH */
#endif
    for (i = 0; i < vqrows; i++) {
	/* Find r = a random, previously unselected row from the input */

#ifndef WIN32			/* RAH */
	r = (random() & (int32)0x7fffffff) % rows;
#else  /* RAH */
	r = (rand() & (int32)0x7fffffff) % rows; /* RAH */
#endif /* RAH */
	while (bitvec_is_set (sel, r)) {	/* BUG: possible infinite loop!! */
	    if (++r >= rows)
		r = 0;
	}
	bitvec_set (sel, r);
	
	memcpy ((void *)(mean[i]), (void *)(data[r]), cols * sizeof(float32));
	/* BUG: What if two randomly selected rows are identical in content?? */
    }
    bitvec_free (sel);
    
    count = (int32 *) ckd_calloc (vqrows, sizeof(int32));
    
    /* In k-means, unmapped means in any iteration are a problem.  Replace them with gmean */
    gmean = (float32 *) ckd_calloc (cols, sizeof(float32));
    vector_mean (gmean, mean, vqrows, cols);

    for (it = 0;; it++) {		/* Iterations of k-means algorithm */
	/* Find the current data->mean mappings (labels) */
	sqerr = 0.0;
	for (i = 0; i < rows; i++) {
	    map[i] = vector_vqlabel (data[i], mean, vqrows, cols, &t);
	    sqerr += t;
	}
	ptmr_stop(&tm);
	
	if (it == 0)
	    E_INFO("Iter %4d: %.1fs CPU; sqerr= %e\n", it, tm.t_cpu, sqerr);
	else
	    E_INFO("Iter %4d: %.1fs CPU; sqerr= %e; delta= %e\n",
		   it, tm.t_cpu, sqerr, (prev_sqerr-sqerr)/prev_sqerr);
	
	/* Check if exit condition satisfied */
	if ((sqerr == 0.0) || (it >= maxiter-1) ||
	    ((it > 0) && ( ((prev_sqerr - sqerr) / prev_sqerr) < epsilon )) )
	    break;
	prev_sqerr = sqerr;
	
	ptmr_start(&tm);
	
	/* Update (reestimate) means */
	for (i = 0; i < vqrows; i++) {
	    for (j = 0; j < cols; j++)
		mean[i][j] = 0.0;
	    count[i] = 0;
	}
	for (i = 0; i < rows; i++) {
	    vector_accum (mean[map[i]], data[i], cols);
	    count[map[i]]++;
	}
	for (i = 0; i < vqrows; i++) {
	    if (count[i] > 1) {
		t = 1.0 / (float64)(count[i]);
		for (j = 0; j < cols; j++)
		  /*		  mean[i][j] *= t; */ /* RAH, compiler was complaining about this,  */
		  mean[i][j] = (float32) ((float64) mean[i][j] * (float64) t); /*  */
	    } else if (count[i] == 0) {
		E_ERROR("Iter %d: mean[%d] unmapped\n", it, i);
		memcpy (mean[i], gmean, cols * sizeof(float32));
	    }
	}
    }
    
    ckd_free (count);
    ckd_free (gmean);
    
    return sqerr;
}