Help with IQ balance equations?
I wrote code in Java based on that article but it isn't working for me. Alpha (I-data scalar) calculates to be around $100$ and $cos(\omega)$ often calculates to an imaginary number. Did anyone have any luck with this or could point me to a better IQ Correction algorithm? 

(To be clear: calculateMatrix, a, c, and d are all instance variables)

public void correctIQ(int[] data) {

    int length = data.length / 2;

    // ( I'(t) &amp; Q'(t) )
    int[] iData = new int[length];
    int[] qData = new int[length];

    // Deinterleave IQ data 
    for (int i = 0; i &lt; length; i++) {
        iData[i] = data[i * 2];
        qData[i] = data[i * 2 + 1];
    }

    // ( I''(t) &amp; Q''(t) )
    double[] qDataOffset = new double[length];
    double[] iDataOffset = new double[length];

    // Calculate DC offset
    double qOffset = mean(qData);
    double iOffset = mean(iData);

    // Apply DC offset
    for (int i = 0; i &lt; length; i++) {
        iDataOffset[i] = iData[i] - iOffset;
        qDataOffset[i] = qData[i] - qOffset;
    }

    // Provided equations calculated on frequency change
    if(calculateMatrix) {
        double alpha = Math.sqrt(2.0 * multiplyMean(iDataOffset, iDataOffset));
        double sinw = (2.0 / alpha) * multiplyMean(iDataOffset, qDataOffset);
        double cosw = Math.sqrt(1.0 - (sinw * sinw));

        a = 1.0f / alpha;
        c = -sinw / (alpha * cosw);
        d = 1.0f / cosw;
    }        

    // Replace into interleaved array
    for(int i=0;i&lt;length;i++) {
        data[i*2]=(int)((iData[i]-iOffset)*a);
        data[i*2+1]=(int)((iData[i]-iOffset)*c+(qData[i]-qOffset)*d);
    }
}

double mean(int[] values) {
    long sum = 0L;
    for(int i=0;i&lt;values.length;i++) {
        sum+=values[i];
    }
    return ((double)sum)/values.length;
}

double multiplyMean(double[] values1, double[] values2) {
    long sum = 0L;
    for(int i=0;i&lt;values1.length;i++) {
        sum+=values1[i]*values2[i];
    }
    return ((double)sum)/values1.length;
}