Ejemplo n.º 1
0
void MainWidgetTFM::initTextures()
{
	
    // Read CSV file
    std::ifstream ifile(filename);
	std::string line;

	// x is current receiving element
	// t is current time sample
	// p is current pulsing element
	int x = 0, t = 0, p = 0;
	if (ifile.is_open()){
		double tmp;
		while(ifile >> tmp){
			if(t % 100 == 0)
				qDebug() << "Reading CSV: " << (float)t * 100.0 / sampleSize << "%";
			//std::cout << "temp: " << tmp << std::endl;
			m_data[index3d(t,x,p)] = tmp; // (fabs(tmp));
			//std::cout << "m_data: " << m_data[x][t] << std::endl;
			x++;
			for (int j = 1; j < numElements; j++) {
				ifile >> tmp;
				m_data[index3d(t,x,p)] = tmp; // fabs(tmp);
				//m_data[x][t] = (uchar)255;
				x++;
			}
			p++;
			x = 0;
			for(int i = 1; i < numElements; i++){
				for (int j = 0; j < numElements; j++) {
					ifile >> tmp;
					m_data[index3d(t,x,p)] = tmp; // fabs(tmp);
					//m_data[x][t] = (uchar)255;
					x++;
				}
				x = 0;
				p++;
			}
			x = 0;
			p = 0;
			t++;
		}
		ifile.close();
	}
Ejemplo n.º 2
0
void mexFunction(int lhsCount, mxArray* lhs[], int rhsCount, const mxArray* rhs[])
{
	mxArray* pi_R;
	unsigned int* pi_R_data;
	unsigned int* pi_R_j_data;
	unsigned int pi_R_j_idx;
	unsigned int* inv_pi = (unsigned int*)mxGetData(rhs[0]);
	size_t max_t = mxGetM(rhs[0]);
	size_t N = mxGetN(rhs[0]);
	unsigned int* t = (unsigned int*)mxGetData(rhs[1]);
	unsigned int n = (unsigned int)mxGetScalar(rhs[2]);
	char* already_ranked = mxMalloc(n * sizeof(char));
	unsigned int* this_inv_pi;

	unsigned int i, j, k;
	unsigned int row;

	mwSize dims[3];
	dims[0] = n - 1;
	dims[1] = max_t;
	dims[2] = N;

	pi_R = mxCreateNumericArray(3, dims, mxUINT32_CLASS, mxREAL);
	pi_R_data = (unsigned int*)mxGetData(pi_R);
	lhs[0] = pi_R;

	for (i = 0; i < N; ++i)
	{
		this_inv_pi = inv_pi + index2d(0, max_t, i);
		memset(already_ranked, 0, n * sizeof(char));
		for (j = 0; j < t[i]; ++j)
		{
			/* Get this sparse vector of R */
			pi_R_j_data = pi_R_data + index3d(0, n - 1, j, max_t, i);
			pi_R_j_idx = 0;

			/* Fill vector with proper entries */
			/* Only row will be inv_pi[j] */
			row = this_inv_pi[j];
			already_ranked[row] = 1;
			for (k = 0; k < n; ++k)
			{
				/* If not already ranked, and not current item being ranked, mark in vector */
				if (already_ranked[k] == 0)
				{
					pi_R_j_data[pi_R_j_idx++] = k * n + row;
				}
			}
			assert(pi_R_j_idx == n - j - 1);
		}
	}

	mxFree(already_ranked);
}
Ejemplo n.º 3
0
void MainWidgetTFM::filterFFT(){

	for (int pulsing = 0; pulsing < numElements; pulsing++) {
		if(pulsing % 10 == 0)
		qDebug() << "filtering: " << ((float)pulsing) * 100.0/((float)numElements) << "%";
	for (int element = 0; element < numElements; element++) {

		// copy the array to fftw_complex structure, could be a faster way e.g. memcpy
		for (int i = 0; i < sampleSize; i++) {
			in[i][0] = m_data[index3d(i,element,pulsing)];
			in[i][1] = 0;
		}
		//for (int i = 0; i < 100; i++) std::cout << i << ": " << in[i][0] << std::endl;

		/* FFT */

		fftw_plan my_plan = fftw_plan_dft_1d(sampleSize, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
		fftw_execute(my_plan);

		for (int i = 0; i < sampleSize; i++) {
			out[i][0] /= (float)sampleSize;
			out[i][1] /= (float)sampleSize;
			//std::cout << i << ": " << out[i][0] << " , " << out[i][1] << "i" << std::endl;
		}
		for (int i = 0; i < sampleSize; i++) {
			//magnitude[i] = sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]);
			//std::cout << i << ": " << magnitude[i] << std::endl;
			//outfile << to_string(magnitude[i]) << endl;
		}

		//int peak_frequency = 0;
		//double peak_val = 0.0;

		//for(int i = 0; i < 500; i++){
		//	//std::cout << i << ": " << magnitude[i] << std::endl;

		//	if(magnitude[i] > peak_val){
		//		std::cout << "new peak : " << i << std::endl;
		//		peak_val = magnitude[i];
		//		peak_frequency = i;
		//	}
		//}

		//std::cout << "peak_frequency(index) : " << peak_frequency << std::endl;
		//float time_period = 1.0 / peak_frequency;
		

		fftw_destroy_plan(my_plan);


		/* FILTERING */

		// Create gaussian filter vector
		// f(x; sig, c) = e ^ ( -(x-c)^2 / 2*sig^2 )
		//float sigma, c;
		//createGaussian(70, 200);
		createGaussian();
		double weight = 2.0;
		
		for (int i = 0; i < sampleSize; i++) {
			out[i][0] = out[i][0] * gaussFilter[i] * weight;
			out[i][1] = out[i][1] * gaussFilter[i] * weight;
		}
		for (int i = 500; i < sampleSize; i++) {
			//out[i][0] = 0.0;
			//out[i][1] = 0.0;
		}
		
		/* INVERSE FFT */

		// To check that there's no existing data
		for (int i = 0; i < sampleSize; i++) {
			in[i][0] = 0;
			in[i][1] = 0;
		}
		
		my_plan = fftw_plan_dft_1d(sampleSize, out, in, FFTW_BACKWARD, FFTW_ESTIMATE);
		fftw_execute(my_plan);
		//for (int i = 0; i < 100; i++) std::cout << i << ": " << in[i][0] << std::endl;
		
		//for (int i = 0; i < sampleSize; i++) std::cout << i << ": " << in[i][0] << " , " << in[i][1] << "i" << std::endl;


		fftw_destroy_plan(my_plan);
		
		// multiply output by 2 (why?)
		for (int i = 0; i < sampleSize; i++) {
			in[i][0] *= 2.0;
			in[i][1] *= 2.0;
			//qDebug() << in[i][0];
			//qDebug() << in[i][1];
		}


		// Calc absolute value and put back into array
		for (int i = 0; i < sampleSize; i++) {
			//m_filtered_data_ri[element][i][0] = (in[i][0]);//(float)
			//m_filtered_data_ri[element][i][1] = (in[i][1]);//(float)
			m_filtered_data_ri[index3d(i,element,pulsing)*2] 		= (in[i][0]);//(float)
			m_filtered_data_ri[index3d(i,element,pulsing)*2+1]	= (in[i][1]);//(float)
		}

	}
	}

	// calculate in seconds, using sampling rate
	//time_period = time_period / samplingRate;
	//std::cout << "test" << std::endl;
	//float frequency = 1 / time_period;
	//std::cout << "peak frequency : " << frequency << std::endl;
}