Esempio n. 1
0
/**
 * returns a diagonal matrix
 * @param  v a vector
 * @return a diagonal matrix with the given vector v on the diagonal
 */
Matrix Diag(const Matrix& v)
{
  Matrix res;
  if (v.GetCols() == 1)
  {
    // the given matrix is a vector n x 1
    int rows = v.GetRows();
    res = Matrix(rows, rows);

    // copy the values of the vector to the matrix
    for (int r=1; r <= rows; r++)
    {
      res(r, r) = v.get(r, 1);
    }
  }
  else if (v.GetRows() == 1)
  {
    // the given matrix is a vector 1 x n
    int cols = v.GetCols();
    res = Matrix(cols, cols);

    // copy the values of the vector to the matrix
    for (int c=1; c <= cols; c++)
    {
      res(c, c) = v.get(1, c);
    }
  }
  else
  {
    throw Exception("Parameter for diag must be a vector");
  }
  return res;
}
Esempio n. 2
0
// / Calculates the mean value of the elements in a matrix.
double mean(Matrix < double >&m)
{
    double sum = 0.;

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += m[r][c];
    return sum / double (m.GetRows() * m.GetCols());
}
Esempio n. 3
0
Complex scalprod(Matrix < Complex > &m1, Matrix < double >&m2)
{
    assert((m1.GetRows() == m2.GetRows()) && (m1.GetCols() == m2.GetCols()));
    Complex sum(0, 0);

    for (int r = 0; r < m1.GetRows(); r++)
        for (int c = 0; c < m1.GetCols(); c++)
            sum += m1[r][c] * m2[r][c];
    return sum;
}
Esempio n. 4
0
// / Calculates the varaince of the elements in a matrix.
double variance(Matrix < double >&m)
{
    double sum = 0.;

    double average = mean(m);

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += (m[r][c] - average) * (m[r][c] - average);
    return sum / double (m.GetRows() * m.GetCols() - 1);
}
Esempio n. 5
0
Complex scalconj(Matrix < Complex > &m1, Matrix < Complex > &m2)        // total(A.B*)
{
    assert((m1.GetRows() == m2.GetRows()) && (m1.GetCols() == m2.GetCols()));
    Complex sum(0, 0);

    for (int r = 0; r < m1.GetRows(); r++)
        for (int c = 0; c < m1.GetCols(); c++)
            sum += m1[r][c] * conj(m2[r][c]);
    return sum;

}
Esempio n. 6
0
Matrix fft2(Matrix &m, int isign)
{
    assert(m.bComplex == TRUE);
    int ndim = 2;	
    long row = m.GetRows(); 
    long col = m.GetCols();
	
    Matrix ret(m);
    unsigned long nn[3];
		
    nn[1] = row;
    nn[2] = col / 2;
		
#if 1
    float *pdata = new float[row * col + 1];
    memcpy( pdata + 1, m.p, sizeof(float) * row * col);
    fourn( pdata, nn, ndim, isign );
    memcpy( ret.p, pdata + 1, sizeof(float) * row * col);
    delete[] pdata;
#else
	float* pdata = new float[row * col];
	memcpy(pdata, m.p, sizeof(float) * row * col);
	CUFFT::fft(pdata, col / 2, row, isign == 1);
	CUFFT::fft(pdata, col / 2, row, isign != 1);
	memcpy(ret.p, pdata, sizeof(float) * row * col);
	delete [] pdata;
#endif
    ret.bComplex = TRUE;
		
    return ret;	
}
Esempio n. 7
0
Matrix WhitenFrame(Matrix &m)
{
    int nRow = m.GetRows();
    int nCol = m.GetCols();
    assert(nRow == nCol);
	
    Matrix fx(nRow), fy(nRow);
    Meshgrid(fx, -nRow / 2, nRow / 2 - 1, fy, -nRow / 2, nRow / 2 - 1);
    Matrix theta = fx;
    Matrix rho = fx;
    
    cart2pol( fx,fy,theta,rho );
	
#if 1
    Matrix complex = Complex(m);
    Matrix fftm = fft2(complex, 1);
    Matrix imF = fftshift(fftm);
    Matrix time = times(rho, imF);
    Matrix imW = fftshift(time);
#else
    Matrix complex = Complex(m);
    Matrix fftm = fft2(complex, 1);
    Matrix imF = fftshift(fftm);
    Matrix time = times(rho, imF);
    Matrix imW = fftshift(time);
#endif

    return imW;
}
Esempio n. 8
0
void ZeroUnPad(Matrix < Complex > &unpadded, Matrix < Complex > &padded)        // for 
                                                                                // square 
                                                                                // matrices
{
    // remove zero padding
    int unpadded_size = unpadded.GetCols();

    int padded_size = padded.GetCols();

    int low = (padded_size - unpadded_size) / 2;

    int high = (padded_size + unpadded_size) / 2;

    for (int ii = low; ii < high; ii++)
        for (int jj = low; jj < high; jj++)
            unpadded[ii - high][jj - high] = padded[ii][jj];
}
Esempio n. 9
0
Complex total(Matrix < Complex > &m)
{
    Complex sum(0, 0);

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += m[r][c];
    return sum;
}
Esempio n. 10
0
void ZeroPad(Matrix < Complex > &unpadded, Matrix < Complex > &padded)  // for 
                                                                        // square 
                                                                        // matrices
{
    // Pad a matrix to padding x padding
    int unpadded_size = unpadded.GetCols();

    int padded_size = padded.GetCols();

    int low = (padded_size - unpadded_size) / 2;

    int high = (padded_size + unpadded_size) / 2;

    for (int ii = low; ii < high; ii++)
        for (int jj = low; jj < high; jj++)
            padded[ii][jj] = unpadded[ii - low][jj - low];

}
Esempio n. 11
0
// / Computes the sum of all of the elements in a matrix.
double total(Matrix < double >&m)
{
    double sum = 0.;

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += m[r][c];
    return sum;

}
Esempio n. 12
0
double norm2(Matrix < Complex > &m)
{
    double sum = 0.;

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += norm(m[r][c]);
    return sum;

}
Esempio n. 13
0
	int ExtractCode( Image < uint8 > & DATANOR, Code < float > & codedata, Matrix < float > & Feat,
		 struct osiris_parameter & param, Filtres < float > & Gabor, Appoints & points )
		 {

		   Feat = Featextract( DATANOR, Gabor, points );
		   codedata.SetSize( Feat.GetRows(), Feat.GetCols() );
		   codedata.TMPLATE = TMextract( Feat );
		   codedata.MASK.init( 1 );

		   return ( 0 );
	}
Esempio n. 14
0
void ShowMatrix(ostream& os, const Matrix<T>& m)
{
  const size_t MaxR = m.GetRows();
  const size_t MaxC = m.GetCols();
  size_t r, c;

  for (r = 0; r < MaxR; ++r)
  {
    os << "[ ";

    for (c = 0; c < MaxC; ++c)
      os << setw(3) << m.Get(r,c) << " ";

    os << "]\r\n";
  }
}
Esempio n. 15
0
bool IfFilterFactory::toBoolean(const Value& value)
{
	bool result = false;

	wchar_t type = value.GetType();
	switch (type)
	{
	case 'i':
		{
			int i = value.GetInteger();
			result = (i != 0);
		}
		break;
	case 'f':
		{
			double f = value.GetFloat();
			result = (f != 0.0 && f == f);
		}
		break;
	case 'b':
		{
			result = value.GetBool();
		}
		break;
	case 's':
		{
			wstring s = value.GetString();
			result = s.length() > 0 && s != L"false" && s != L"0";
		}
		break;
	case 'm':
		{
			Matrix<Value> m = value.GetArray();
			result = m.GetRows() > 0 && m.GetCols() > 0;
		}
		break;
	default:
		result = false;
		break;
	}

	return result;
}
Esempio n. 16
0
// / Converts a matrix composed of doubles into a FITS file
void mat2fits(Matrix < double >&m, const char *filename)
{
    int status = 0;

    fitsfile *fptr;

    long fpixel = 1, naxis = 2, nelements;

    long naxes[2];

    // Initialise storage
    naxes[0] = (long)m.GetRows();
    naxes[1] = (long)m.GetCols();
    nelements = naxes[0] * naxes[1];

    double *ptrimg;

    // Create pointer image
    ptrimg = (double *)malloc(nelements * sizeof(double));

    for (int ii = 0; ii < naxes[0]; ii++)
        for (int jj = 0; jj < naxes[1]; jj++)
            ptrimg[ ii + jj * naxes[0] ] = m[ii][jj];

    // Create new file, write image, then close file
    if (status == 0)
        fits_create_file(&fptr, filename, &status);
    if (status == 0)
        fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
    if (status == 0)
        fits_write_img(fptr, TDOUBLE, fpixel, nelements, &ptrimg[0], &status);
    if (status == 0)
        fits_close_file(fptr, &status);
    free(ptrimg);

    fits_report_error(stderr, status);

}
Esempio n. 17
0
// Function to interpolate a 2d rectangular grid onto another 2d
// rectangular grid with arbitrary scaling, translation and
// rotation. Uses bilinear interpolation and uses periodic boundary
// conditions i.e. wraps the boundaries in the same way data from an
// FFT is wrapped.
// Taken from David Buscher and converted for use with this Matrix class by
// Fabien Baron
void Interp2d(Matrix < double >&dataIn, Matrix < double >&dataOut,
              double offsetX, double offsetY, double scale, double rotation)
{
    // Matrix<double> &dataIn = Input grid of data points from which to
    // extract a subgrid. The grid pitch is assumed to be the same
    // in both dimensions (this restriction only holds if a non-zero rotation
    // is required)
    // Matrix<double> &dataOut = Output gridded values.
    // double offsetX = X offset of (0,0) point in output grid in input grid
    // coordinate units.
    // If greater than nxIn, the coordinates are wrapped in the X dimension.
    // double offsetY = Y offset of (0,0) point in output grid in input grid
    // coordinate units.
    // If greater than nyIn, the coordinates are wrapped in the Y dimension
    // double scale = output grid pitch measured in units of the input grid
    // pitch
    // double rotation = rotation between input and output grids in radians.
    // Positive values rotate the output axes *clockwise* wrt the input axes

    int ix0, ix1, iy0, iy1;

    double x, y, intX0, intX1, intY0, intY1, fracX, fracY;

    int nxIn = dataIn.GetRows();

    int nyIn = dataIn.GetCols();

    int nxOut = dataOut.GetRows();

    int nyOut = dataOut.GetCols();

    double c = scale * cos(rotation);

    double s = scale * sin(rotation);

    for (int iy = 0; iy < nyOut; iy++)
    {
        for (int ix = 0; ix < nxOut; ix++)
        {
            x = c * ix + s * iy + offsetX;
            y = -s * ix + c * iy + offsetY;

            /* Get integer and fractional parts of x position */
            intX0 = floor(x);
            intX1 = intX0 + 1.0;
            fracX = x - intX0;

            /* Do modulo arithmetic correctly for negative numbers */
            ix0 = (int)(intX0 - nxIn * floor(intX0 / nxIn));
            ix1 = (int)(intX1 - nxIn * floor(intX1 / nxIn));

            /* Repeat for y */
            intY0 = floor(y);
            intY1 = intY0 + 1.0;
            fracY = y - intY0;
            iy0 = (int)(intY0 - nyIn * floor(intY0 / nyIn));
            iy1 = (int)(intY1 - nyIn * floor(intY1 / nyIn));

            dataOut[ix][iy] = fracX * (fracY * dataIn[ix1][iy1] 
                           + (1.0 - fracY) * dataIn[ix1][iy0]) 
                           + (1.0 - fracX) * (fracY * dataIn[ix0][iy1] +
                                        (1.0 - fracY) * dataIn[ix0][iy0]);
        }
    }

}
Esempio n. 18
0
// / Converts a matrix composed of complex numbers into a FITS file
void mat2fits(Matrix < Complex > &m, char *filename)  // TBD: fix row major vs colum major
{
    int status = 0;

    fitsfile *fptr;

    long fpixel = 1, naxis = 2, nelements;

    long naxes[2];

    // Initialise storage
    naxes[0] = (long)m.GetRows();
    naxes[1] = (long)m.GetCols();
    nelements = naxes[0] * naxes[1];

    // Filenames
    char fitsreal[100];

    char fitsimag[100];

    for (int i = 0; i < 100; i++)
    {
        fitsreal[i] = '\0';
        fitsimag[i] = '\0';
    }

    strcpy(fitsreal, filename);
    strcpy(fitsimag, filename);
    strcat(fitsreal, "_r");
    strcat(fitsimag, "_i");

    double *ptrimgreal = (double *)malloc(nelements * sizeof(double));

    for (int ii = 0; ii < naxes[0]; ii++)
        for (int jj = 0; jj < naxes[1]; jj++)
            ptrimgreal[ii + jj * naxes[0]] = m[naxes[0] - ii - 1][jj].real();

    // Create new file, write image, then close file
    if (status == 0)
        fits_create_file(&fptr, fitsreal, &status);
    if (status == 0)
        fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
    if (status == 0)
        fits_write_img(fptr, TDOUBLE, fpixel, nelements, &ptrimgreal[0],
                       &status);
    if (status == 0)
        fits_close_file(fptr, &status);

    free(ptrimgreal);

    double *ptrimgimag = (double *)malloc(nelements * sizeof(double));

    for (int ii = 0; ii < naxes[0]; ii++)
        for (int jj = 0; jj < naxes[1]; jj++)
            ptrimgimag[ii + jj * naxes[0]] = m[ii][jj].imag();

    // Create new file, write image, then close file
    if (status == 0)
        fits_create_file(&fptr, fitsimag, &status);
    if (status == 0)
        fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
    if (status == 0)
        fits_write_img(fptr, TDOUBLE, fpixel, nelements, &ptrimgimag[0],
                       &status);
    if (status == 0)
        fits_close_file(fptr, &status);

    free(ptrimgimag);

    fits_report_error(stderr, status);

}
Esempio n. 19
0
void FFT(Matrix < Complex > &min, Matrix < Complex > &mout, int direction, int dccenter)
{
    assert(min.GetCols() == min.GetRows());     // square matrix
    assert(mout.GetCols() == mout.GetRows());
    assert(mout.GetCols() == min.GetRows());
    int nfft = min.GetCols();

    fftw_complex *in =
        (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * nfft * nfft);
    fftw_complex *out =
        (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * nfft * nfft);

    for (int iy = 0; iy < nfft; iy++)
    {
        for (int ix = 0; ix < nfft; ix++)
        {
            in[ix + nfft * iy][0] = min[ix][iy].real();
            in[ix + nfft * iy][1] = min[ix][iy].imag();
        }
    }

    fftw_plan p;

    if (direction >= 0)
        p = fftw_plan_dft_2d(nfft, nfft, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    else
        p = fftw_plan_dft_2d(nfft, nfft, in, out, FFTW_BACKWARD,
                             FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    fftw_free(in);

    if (dccenter == 0)
    {
        for (int iy = 0; iy < nfft; iy++)
            for (int ix = 0; ix < nfft; ix++)
                mout[ix][iy]
                    = Complex(out[ix + nfft * iy][0], out[ix + nfft * iy][1])
                    / double (nfft);
    }
    else
    {
        for (int iy = 0; iy < nfft / 2; iy++)
        {
            for (int ix = 0; ix < nfft / 2; ix++)
            {
                mout[ix][iy] =
                    Complex(out[nfft / 2 + ix + nfft * (iy + nfft / 2)][0],
                            out[nfft / 2 + ix +
                                nfft * (iy + nfft / 2)][1]) / double (nfft);
            }

            for (int ix = nfft / 2; ix < nfft; ix++)
            {
                mout[ix][iy] =
                    Complex(out[ix - nfft / 2 + nfft * (iy + nfft / 2)][0],
                            out[ix - nfft / 2 +
                                nfft * (iy + nfft / 2)][1]) / double (nfft);
            }
        }

        for (int iy = nfft / 2; iy < nfft; iy++)
        {
            for (int ix = 0; ix < nfft / 2; ix++)
            {
                mout[ix][iy] =
                    Complex(out[ix + nfft / 2 + nfft * (iy - nfft / 2)][0],
                            out[ix + nfft / 2 +
                                nfft * (iy - nfft / 2)][1]) / double (nfft);
            }

            for (int ix = nfft / 2; ix < nfft; ix++)
            {
                mout[ix][iy] =
                    Complex(out[ix - nfft / 2 + nfft * (iy - nfft / 2)][0],
                            out[ix - nfft / 2 +
                                nfft * (iy - nfft / 2)][1]) / double (nfft);
            }
        }
    }
    fftw_free(out);

}