Exemplo n.º 1
0
/** \return GL_TRUE for pass, GL_FALSE for fail */
static GLboolean
test_fbo(const struct format_info *info)
{
   const int max = get_max_val(info);
   const int comps = num_components(info->BaseFormat);
   const GLenum type = get_datatype(info);
   GLint f;
   GLuint fbo, texObj;
   GLenum status;
   GLboolean intMode;
   GLint buf;

   if (0)
      fprintf(stderr, "============ Testing format = %s ========\n", info->Name);

   /* Create texture */
   glGenTextures(1, &texObj);
   glBindTexture(GL_TEXTURE_2D, texObj);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

   glTexImage2D(GL_TEXTURE_2D, 0, info->IntFormat, TexWidth, TexHeight, 0,
                info->BaseFormat, type, NULL);

   if (check_error(__FILE__, __LINE__))
      return GL_FALSE;

   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &f);
   assert(f == info->IntFormat);


   /* Create FBO to render to texture */
   glGenFramebuffers(1, &fbo);
   glBindFramebuffer(GL_FRAMEBUFFER, fbo);
   glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                          GL_TEXTURE_2D, texObj, 0);

   if (check_error(__FILE__, __LINE__))
      return GL_FALSE;

   status = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
   if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
      fprintf(stderr, "%s: failure: framebuffer incomplete.\n", TestName);
      return GL_FALSE;
   }


   glGetBooleanv(GL_RGBA_INTEGER_MODE_EXT, &intMode);
   if (check_error(__FILE__, __LINE__))
      return GL_FALSE;
   if (!intMode) {
      fprintf(stderr, "%s: GL_RGBA_INTEGER_MODE_EXT return GL_FALSE\n",
              TestName);
      return GL_FALSE;
   }

   glGetIntegerv(GL_READ_BUFFER, &buf);
   assert(buf == GL_COLOR_ATTACHMENT0_EXT);
   glGetIntegerv(GL_DRAW_BUFFER, &buf);
   assert(buf == GL_COLOR_ATTACHMENT0_EXT);


   /* test clearing */
   if (1) {
      static const GLint clr[4] = { 8, 7, 6, 5 };
      GLint pix[4], i;

      glClearColorIiEXT(clr[0], clr[1], clr[2], clr[3]);
      glClear(GL_COLOR_BUFFER_BIT);

      glReadPixels(5, 5, 1, 1, GL_RGBA_INTEGER_EXT, GL_INT, pix);

      for (i = 0; i < comps; i++) {
         if (pix[i] != clr[i]) {
            fprintf(stderr, "%s: glClear failed\n", TestName);
            fprintf(stderr, "  Texture format = %s\n", info->Name);
            fprintf(stderr, "  Expected %d, %d, %d, %d\n",
                    clr[0], clr[1], clr[2], clr[3]);
            fprintf(stderr, "  Found %d, %d, %d, %d\n",
                    pix[0], pix[1], pix[2], pix[3]);
            return GL_FALSE;
         }
      }
   }


   /* Do glDraw/ReadPixels test */
   if (1) {
#define W 15
#define H 10
      GLint image[H * W * 4], readback[H * W * 4];
      GLint i;

      if (info->Signed) {
         for (i = 0; i < W * H * 4; i++) {
            image[i] = (i - 10) % max;
            assert(image[i] < max);
         }
      }
      else {
         for (i = 0; i < W * H * 4; i++) {
            image[i] = (i + 3) % max;
            assert(image[i] < max);
         }
      }

      glUseProgram(PassthroughProgram);
      if(0)glUseProgram(SimpleProgram);

      glWindowPos2i(1, 1);
      glDrawPixels(W, H, GL_RGBA_INTEGER_EXT, GL_INT, image);

      if (check_error(__FILE__, __LINE__))
         return GL_FALSE;

      glReadPixels(1, 1, W, H, GL_RGBA_INTEGER_EXT, GL_INT, readback);

      if (check_error(__FILE__, __LINE__))
         return GL_FALSE;

      for (i = 0; i < W * H * 4; i++) {
         if (readback[i] != image[i]) {
            if (comps == 3 && i % 4 == 3 && readback[i] == 1)
               continue; /* alpha = 1 if base format == RGB */

            fprintf(stderr,
                 "%s: glDraw/ReadPixels failed at %d.  Expected %d, found %d\n",
                    TestName, i, image[i], readback[i]);
            fprintf(stderr, "Texture format = %s\n", info->Name);
            assert(0);
            return GL_FALSE;
         }
      }
#undef W
#undef H
   }

   /* Do rendering test */
   if (1) {
      GLint value[4], result[4], loc, w = piglit_width, h = piglit_height;
      GLint error = 1; /* XXX fix */

      /* choose random value/color for polygon */
      value[0] = rand() % 100;
      value[1] = rand() % 100;
      value[2] = rand() % 100;
      value[3] = rand() % 100;

      glUseProgram(SimpleProgram);
      check_error(__FILE__, __LINE__);

      loc = glGetUniformLocation(SimpleProgram, "value");
      assert(loc >= 0);
      glUniform4iv(loc, 1, value);
      check_error(__FILE__, __LINE__);

#if 0 /* allow testing on mesa until this is implemented */
      loc = glGetFragDataLocationEXT(SimpleProgram, "out_color");
      assert(loc >= 0);
#endif

      glBegin(GL_POLYGON);
      glVertex2f(0, 0);
      glVertex2f(w, 0);
      glVertex2f(w, h);
      glVertex2f(0, h);
      glEnd();
      check_error(__FILE__, __LINE__);

      glReadPixels(w/2, h/2, 1, 1, GL_RGBA_INTEGER, GL_INT, result);
      check_error(__FILE__, __LINE__);

      if (info->BaseFormat == GL_RGB_INTEGER_EXT) {
         value[3] = 1;
      }

      if (abs(result[0] - value[0]) > error ||
          abs(result[1] - value[1]) > error ||
          abs(result[2] - value[2]) > error ||
          abs(result[3] - value[3]) > error) {
         fprintf(stderr, "%s: failure with format %s:\n", TestName, info->Name);
         fprintf(stderr, "  input value = %d, %d, %d, %d\n",
                 value[0], value[1], value[2], value[3]);
         fprintf(stderr, "  result color = %d, %d, %d, %d\n",
                 result[0], result[1], result[2], result[3]);
         return GL_FALSE;
      }
   }

   piglit_present_results();

   glDeleteTextures(1, &texObj);
   glDeleteFramebuffers(1, &fbo);

   return GL_TRUE;
}
// filter the data
void hp_filter(const double *raw_data, const int sigma, double *filtered_data, mwSize nt, mwSize nv)
{
	
	
	int t, hp_mask_size, tt, v, done_c0;
	mxArray *hp_exp_array, *voxel_rawtimeseries_array, *voxel_filteredtimeseries_array;
	double *hp_exp, *voxel_rawtimeseries, c0, *voxel_filteredtimeseries;
	double c, w, A, B, C, D, N, tmpdenom;
	int tt_left, tt_right;
	int dt;
	
	// define the convolution kernel
	hp_mask_size = sigma*3;
	
	hp_exp_array = mxCreateDoubleMatrix(1,(hp_mask_size*2+1),mxREAL);
	hp_exp= mxGetPr(hp_exp_array);
	
	hp_convkernel(hp_exp, hp_mask_size, sigma);
	
	
	// select the time series
	voxel_rawtimeseries_array = mxCreateDoubleMatrix(1,nt, mxREAL);
	voxel_rawtimeseries = mxGetPr(voxel_rawtimeseries_array);
	
	voxel_filteredtimeseries_array = mxCreateDoubleMatrix(1,nt, mxREAL);
	voxel_filteredtimeseries = mxGetPr(voxel_filteredtimeseries_array);
	
	for (v = 0; v < nv; v++)
	{
		//get a column of data
		for (t = 0; t < nt; t++)
		{
			voxel_rawtimeseries[t] = raw_data[v*nt + t];
		}
		
		//initialize done_c0 and c0
		done_c0 = 0;
		c0 = 0;
		
		//loop through the t
		for (t = 0; t < nt; t++)
		{
			//reset these variables
			A=0;
			B=0;
			C=0;
			D=0;
			N=0;
			
			//get the range of convolution for each t
			tt_left = get_max_val(t-hp_mask_size, 0);
			tt_right = get_min_val(t+hp_mask_size, nt-1);
			
			//loop through the convolution
			for(tt=tt_left; tt<=tt_right; tt++)
			{
				dt = tt-t;
				w = hp_exp[dt+hp_mask_size];
				A += w * dt;
				B += w * voxel_rawtimeseries[tt];
				C += w * dt * dt;
				D += w * dt * voxel_rawtimeseries[tt];
				N += w;
			}
			
			// calculate the temporary denominator for t
			tmpdenom=C*N-A*A;
			
			// check that its not zero
			if (tmpdenom != 0)
			{
				// if its not zero, divide c by this value
				c = (B*C-A*D) / tmpdenom;
				// and set done_c0 to 1
				if (done_c0 == 0)
				{
					c0=c;
					done_c0=1;
				}

				voxel_filteredtimeseries[t] = c0 + voxel_rawtimeseries[t] - c;
			}
			else {
				voxel_filteredtimeseries[t] = voxel_rawtimeseries[t];
			}
		} // end t loop
		
		
		for (t = 0; t < nt; t++)
		{
			filtered_data[v*nt+t] = voxel_filteredtimeseries[t];
		}
		
	}
}
Exemplo n.º 3
0
void
TempoTrackV2::calculateBeats(const vector<double> &df,
                             const vector<double> &beat_period,
                             vector<double> &beats)
{
    if (df.empty() || beat_period.empty()) return;

    d_vec_t cumscore(df.size()); // store cumulative score
    i_vec_t backlink(df.size()); // backlink (stores best beat locations at each time instant)
    d_vec_t localscore(df.size()); // localscore, for now this is the same as the detection function

    for (unsigned int i=0; i<df.size(); i++)
    {
        localscore[i] = df[i];
        backlink[i] = -1;
    }

    double tightness = 4.;
    double alpha = 0.9;

    // main loop
    for (unsigned int i=0; i<localscore.size(); i++)
    {
        int prange_min = -2*beat_period[i];
        int prange_max = round(-0.5*beat_period[i]);

        // transition range
        d_vec_t txwt (prange_max - prange_min + 1);
        d_vec_t scorecands (txwt.size());

        for (unsigned int j=0;j<txwt.size();j++)
        {
            double mu = static_cast<double> (beat_period[i]);
            txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2));

            // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J
            // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION:  D_VEC_T SCORECANDS (TXWT.SIZE());

            int cscore_ind = i+prange_min+j;
            if (cscore_ind >= 0)
            {
                scorecands[j] = txwt[j] * cumscore[cscore_ind];
            }
        }

        // find max value and index of maximum value
        double vv = get_max_val(scorecands);
        int xx = get_max_ind(scorecands);

        cumscore[i] = alpha*vv + (1.-alpha)*localscore[i];
        backlink[i] = i+prange_min+xx;

//        std::cerr << "backlink[" << i << "] <= " << backlink[i] << std::endl;
    }

    // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR
    d_vec_t tmp_vec;
    for (unsigned int i=cumscore.size() - beat_period[beat_period.size()-1] ; i<cumscore.size(); i++)
    {
        tmp_vec.push_back(cumscore[i]);
    }

    int startpoint = get_max_ind(tmp_vec) + cumscore.size() - beat_period[beat_period.size()-1] ;

    // can happen if no results obtained earlier (e.g. input too short)
    if (startpoint >= backlink.size()) startpoint = backlink.size()-1;

    // USE BACKLINK TO GET EACH NEW BEAT (TOWARDS THE BEGINNING OF THE FILE)
    //  BACKTRACKING FROM THE END TO THE BEGINNING.. MAKING SURE NOT TO GO BEFORE SAMPLE 0
    i_vec_t ibeats;
    ibeats.push_back(startpoint);
//    std::cerr << "startpoint = " << startpoint << std::endl;
    while (backlink[ibeats.back()] > 0)
    {
//        std::cerr << "backlink[" << ibeats.back() << "] = " << backlink[ibeats.back()] << std::endl;
        int b = ibeats.back();
        if (backlink[b] == b) break; // shouldn't happen... haha
        ibeats.push_back(backlink[b]);
    }

    // REVERSE SEQUENCE OF IBEATS AND STORE AS BEATS
    for (unsigned int i=0; i<ibeats.size(); i++)
    {
        beats.push_back( static_cast<double>(ibeats[ibeats.size()-i-1]) );
    }
}
Exemplo n.º 4
0
/** \return GL_TRUE for pass, GL_FALSE for fail */
static bool
test_format(const struct format_info *info)
{
	const int max = get_max_val(info);
	const int comps = num_components(info->BaseFormat);
	const int texels = TexWidth * TexHeight;
	const GLenum type = get_datatype(info);
	const int w = piglit_width / 10;
	const int h = piglit_height / 10;
	const float error = 2.0 / 255.0; /* XXX fix */
	GLfloat expected[4];
	void *buf;
	int value[4];
	GLfloat result[4], bias[4];
	GLint f;

	/* pick random texture color */
	value[0] = rand() % max;
	value[1] = rand() % max;
	value[2] = rand() % max;
	value[3] = rand() % max;

	/* alloc, fill texture image */
	buf = malloc(comps * texels * info->BitsPerChannel / 8);
	fill_array(comps, texels, buf, info->BitsPerChannel, value);

	glTexImage2D(GL_TEXTURE_2D, 0, info->IntFormat, TexWidth, TexHeight, 0,
					 info->BaseFormat, type, buf);

	/* make sure the teximage call worked */
	if (!piglit_check_gl_error(GL_NO_ERROR))
		return false;

	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &f);

	/* setup expected polygon color */
	expected[0] = 0.25;
	expected[1] = 0.50;
	expected[2] = 0.75;
	expected[3] = 1.00;

	/* need to swizzle things depending on texture format */
	switch (info->BaseFormat) {
	case GL_RGBA_INTEGER_EXT:
		/* nothing */
		break;
	case GL_RGB_INTEGER_EXT:
		value[3] = 0.0;
		break;
	case GL_ALPHA_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = 0.0;
		expected[3] = 0.25;
		value[3] = value[0];
		value[0] = value[1] = value[2] = 0.0;
		break;
	case GL_LUMINANCE_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = 0.25;
		expected[3] = 1.0;
		value[1] = value[2] = value[0];
		value[3] = 1.0;
		break;
	case GL_LUMINANCE_ALPHA_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = 0.25;
		value[3] = value[1];
		value[1] = value[2] = value[0];
		break;
	case GL_RED_INTEGER_EXT:
		expected[0] = expected[1] = expected[2] = expected[3] = 0.25;
		value[1] = value[2] = value[3] = value[0];
		break;
	default:
		;
	}

	/* compute, set test bias */
	bias[0] = expected[0] - value[0];
	bias[1] = expected[1] - value[1];
	bias[2] = expected[2] - value[2];
	bias[3] = expected[3] - value[3];
	glUniform4fv(BiasUniform, 1, bias);

	/* draw */
	glClearColor(0, 1, 1, 0);
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POLYGON);
	glTexCoord2f(0, 0);	glVertex2f(0, 0);
	glTexCoord2f(1, 0);	glVertex2f(w, 0);
	glTexCoord2f(1, 1);	glVertex2f(w, h);
	glTexCoord2f(0, 1);	glVertex2f(0, h);
	glEnd();

	/* test */
	glReadPixels(w/2, h/2, 1, 1, GL_RGBA, GL_FLOAT, result);

	if (fabsf(result[0] - expected[0]) > error ||
		 fabsf(result[1] - expected[1]) > error ||
		 fabsf(result[2] - expected[2]) > error ||
		 fabsf(result[3] - expected[3]) > error) {
		fprintf(stderr, "%s: failure with format %s:\n", TestName,
			piglit_get_gl_enum_name(info->IntFormat));
		fprintf(stderr, "  texture color = %d, %d, %d, %d\n",
				  value[0], value[1], value[2], value[3]);
		fprintf(stderr, "  expected color = %g, %g, %g, %g\n",
				  expected[0], expected[1], expected[2], expected[3]);
		fprintf(stderr, "  result color = %g, %g, %g, %g\n",
				  result[0], result[1], result[2], result[3]);
		return false;
	}

	piglit_present_results();

	free(buf);

	return GL_TRUE;
}
Exemplo n.º 5
0
void
TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &beat_period, d_vec_t &tempi)
{
    // following Kevin Murphy's Viterbi decoding to get best path of
    // beat periods through rfcmat

    // make transition matrix
    d_mat_t tmat;
    for (unsigned int i=0;i<wv.size();i++)
    {
        tmat.push_back ( d_vec_t() ); // adds a new column
        for (unsigned int j=0; j<wv.size(); j++)
        {
            tmat[i].push_back(0.); // fill with zeros initially
        }
    }

    // variance of Gaussians in transition matrix
    // formed of Gaussians on diagonal - implies slow tempo change
    double sigma = 8.;
    // don't want really short beat periods, or really long ones
    for (unsigned int i=20;i <wv.size()-20; i++)
    {
        for (unsigned int j=20; j<wv.size()-20; j++)
        {
            double mu = static_cast<double>(i);
            tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) );
        }
    }

    // parameters for Viterbi decoding... this part is taken from
    // Murphy's matlab

    d_mat_t delta;
    i_mat_t psi;
    for (unsigned int i=0;i <rcfmat.size(); i++)
    {
        delta.push_back( d_vec_t());
        psi.push_back( i_vec_t());
        for (unsigned int j=0; j<rcfmat[i].size(); j++)
        {
            delta[i].push_back(0.); // fill with zeros initially
            psi[i].push_back(0); // fill with zeros initially
        }
    }


    unsigned int T = delta.size();

    if (T < 2) return; // can't do anything at all meaningful

    unsigned int Q = delta[0].size();

    // initialize first column of delta
    for (unsigned int j=0; j<Q; j++)
    {
        delta[0][j] = wv[j] * rcfmat[0][j];
        psi[0][j] = 0;
    }

    double deltasum = 0.;
    for (unsigned int i=0; i<Q; i++)
    {
        deltasum += delta[0][i];
    }
    for (unsigned int i=0; i<Q; i++)
    {
        delta[0][i] /= (deltasum + EPS);
    }


    for (unsigned int t=1; t<T; t++)
    {
        d_vec_t tmp_vec(Q);

        for (unsigned int j=0; j<Q; j++)
        {
            for (unsigned int i=0; i<Q; i++)
            {
                tmp_vec[i] = delta[t-1][i] * tmat[j][i];
            }

            delta[t][j] = get_max_val(tmp_vec);

            psi[t][j] = get_max_ind(tmp_vec);

            delta[t][j] *= rcfmat[t][j];
        }

        // normalise current delta column
        double deltasum = 0.;
        for (unsigned int i=0; i<Q; i++)
        {
            deltasum += delta[t][i];
        }
        for (unsigned int i=0; i<Q; i++)
        {
            delta[t][i] /= (deltasum + EPS);
        }
    }

    i_vec_t bestpath(T);
    d_vec_t tmp_vec(Q);
    for (unsigned int i=0; i<Q; i++)
    {
        tmp_vec[i] = delta[T-1][i];
    }

    // find starting point - best beat period for "last" frame
    bestpath[T-1] = get_max_ind(tmp_vec);

    // backtrace through index of maximum values in psi
    for (unsigned int t=T-2; t>0 ;t--)
    {
        bestpath[t] = psi[t+1][bestpath[t+1]];
    }

    // weird but necessary hack -- couldn't get above loop to terminate at t >= 0
    bestpath[0] = psi[1][bestpath[1]];

    unsigned int lastind = 0;
    for (unsigned int i=0; i<T; i++)
    {
        unsigned int step = 128;
        for (unsigned int j=0; j<step; j++)
        {
            lastind = i*step+j;
            beat_period[lastind] = bestpath[i];
        }
//        std::cerr << "bestpath[" << i << "] = " << bestpath[i] << " (used for beat_periods " << i*step << " to " << i*step+step-1 << ")" << std::endl;
    }

    //fill in the last values...
    for (unsigned int i=lastind; i<beat_period.size(); i++)
    {
        beat_period[i] = beat_period[lastind];
    }

    for (unsigned int i = 0; i < beat_period.size(); i++)
    {
        tempi.push_back((60. * m_rate / m_increment)/beat_period[i]);
    }
}