CodecStatus_t Codec_MmeAudio_c::GetAttribute(const char *Attribute, PlayerAttributeDescriptor_t *Value)
{
	//report( severity_error, "Codec_MmeAudio_c::GetAttribute Enter\n");
	if (0 == strcmp(Attribute, "sample_frequency"))
	{
		enum eAccFsCode SamplingFreqCode = (enum eAccFsCode) AudioDecoderStatus.SamplingFreq;
		Value->Id                        = SYSFS_ATTRIBUTE_ID_INTEGER;
		if (SamplingFreqCode < ACC_FS_reserved)
			Value->u.Int = Codec_MmeAudio_c::ConvertCodecSamplingFreq(SamplingFreqCode);
		else
			Value->u.Int = 0;
		return CodecNoError;
	}
	else if (0 == strcmp(Attribute, "number_channels"))
	{
#if 0
		int lfe_mask = ((int) ACC_MODE20t_LFE - (int) ACC_MODE20t);
		int low_channel = 0;
		int number_channels = 0;
		Value->Id                        = SYSFS_ATTRIBUTE_ID_INTEGER;
		if (AudioDecoderStatus.DecAudioMode & lfe_mask) low_channel = 1;
		number_channels = GetNumberOfChannelsFromAudioConfiguration((enum eAccAcMode)(AudioDecoderStatus.DecAudioMode & ~lfe_mask));
		Value->u.Int = number_channels + low_channel;
#else
#define C(f,b) case ACC_MODE ## f ## b: Value->u.ConstCharPointer = #f "/" #b ".0"; break; case ACC_MODE ## f ## b ## _LFE: Value->u.ConstCharPointer = #f "/" #b ".1"; break
#define Cn(f,b) case ACC_MODE ## f ## b: Value->u.ConstCharPointer = #f "/" #b ".0"; break
#define Ct(f,b) case ACC_MODE ## f ## b ## t: Value->u.ConstCharPointer = #f "/" #b ".0"; break; case ACC_MODE ## f ## b ## t_LFE: Value->u.ConstCharPointer = #f "/" #b ".1"; break
		Value->Id = SYSFS_ATTRIBUTE_ID_CONSTCHARPOINTER;
		switch ((enum eAccAcMode)(AudioDecoderStatus.DecAudioMode))
		{
				Ct(2, 0);
				C(1, 0);
				C(2, 0);
				C(3, 0);
				C(2, 1);
				C(3, 1);
				C(2, 2);
				C(3, 2);
				C(2, 3);
				C(3, 3);
				C(2, 4);
				C(3, 4);
				Cn(4, 2);
				Cn(4, 4);
				Cn(5, 2);
				Cn(5, 3);
			default:
				Value->u.ConstCharPointer = "UNKNOWN";
		}
#endif
		return CodecNoError;
	}
	else
	{
		CODEC_ERROR("This attribute does not exist.\n");
		return CodecError;
	}
	return CodecNoError;
}
void RectangleHandler::initRectangle(const Eigen::VectorXd& Sw, double lambda,
		const Eigen::VectorXd& z, Eigen::VectorXd& shapeParams,
		Eigen::VectorXd& F) {

	//Get the points
	Eigen::Vector3d m1(z[0], z[1], 1);
	Eigen::Vector3d m2(z[2], z[3], 1);
	Eigen::Vector3d m3(z[4], z[5], 1);
	Eigen::Vector3d m4(z[6], z[7], 1);

	Eigen::Vector3d Ct(Sw[0], Sw[1], Sw[2]);
	Eigen::Quaterniond Cq(Sw[3], Sw[4], Sw[5], Sw[6]);

	//compute normals
	double c2 = (m1.cross(m3).transpose() * m4)[0]
			/ (m2.cross(m3).transpose() * m4)[0];
	double c3 = (m1.cross(m3).transpose() * m2)[0]
			/ (m4.cross(m3).transpose() * m2)[0];

	Eigen::Vector3d n2 = c2 * m2 - m1;
	Eigen::Vector3d n3 = c3 * m4 - m1;

	//Compute rotation matrix columns
	Eigen::Vector3d R1 = _K.inverse() * n2;
	R1 = R1 / R1.norm();

	Eigen::Vector3d R2 = _K.inverse() * n3;
	R2 = R2 / R2.norm();

	Eigen::Vector3d R3 = R1.cross(R2);

	//Compute frame quaternion
	Eigen::Matrix3d R;
	R << R1, R2, R3;

	const Eigen::Quaterniond qOC(R);

	Eigen::Quaterniond Fqhat = Cq * qOC;

	//Compute frame transaltion
	Eigen::Matrix3d omega = _K.transpose().inverse() * _K.inverse();
	double ff = sqrt(
			(n2.transpose() * omega * n2)[0] / (n3.transpose() * omega * n3)[0]);

	Eigen::Vector3d CtOhat = lambda * _K.inverse() * m1;
	Eigen::Vector3d Fthat = Ct + Cq.toRotationMatrix() * CtOhat;

	//compute shape parameters
	Eigen::Vector3d X = _K * R1;
	Eigen::Vector3d Y = c2 * lambda * m2 - lambda * m1;

	double w = ((X.transpose() * X).inverse() * X.transpose() * Y)[0];
	double h = w / ff;

	//Write the results
	shapeParams << w, h;
	F << Fthat[0], Fthat[1], Fthat[2], Fqhat.w(), Fqhat.x(), Fqhat.y(), Fqhat.z();

}
Exemple #3
0
double ung_ssm::psi_filter(const ugg_ssm& approx_model,
  const double approx_loglik, const arma::vec& scales,
  const unsigned int nsim, arma::cube& alpha, arma::mat& weights,
  arma::umat& indices) {
  
  arma::mat alphahat(m, n + 1);
  arma::cube Vt(m, m, n + 1);
  arma::cube Ct(m, m, n + 1);
  approx_model.smoother_ccov(alphahat, Vt, Ct);
  conditional_cov(Vt, Ct);
  
  std::normal_distribution<> normal(0.0, 1.0);
  
  
  for (unsigned int i = 0; i < nsim; i++) {
    arma::vec um(m);
    for(unsigned int j = 0; j < m; j++) {
      um(j) = normal(engine);
    }
    alpha.slice(i).col(0) = alphahat.col(0) + Vt.slice(0) * um;
  }
  
  std::uniform_real_distribution<> unif(0.0, 1.0);
  arma::vec normalized_weights(nsim);
  double loglik = 0.0;
  if(arma::is_finite(y(0))) {
    weights.col(0) = arma::exp(log_weights(approx_model, 0, alpha) - scales(0));
    double sum_weights = arma::accu(weights.col(0));
    if(sum_weights > 0.0){
      normalized_weights = weights.col(0) / sum_weights;
    } else {
      return -std::numeric_limits<double>::infinity();
    }
    loglik = approx_loglik + std::log(sum_weights / nsim);
  } else {
    weights.col(0).ones();
    normalized_weights.fill(1.0 / nsim);
    loglik = approx_loglik;
  }
  
  for (unsigned int t = 0; t < n; t++) {
    arma::vec r(nsim);
    for (unsigned int i = 0; i < nsim; i++) {
      r(i) = unif(engine);
    }
    indices.col(t) = stratified_sample(normalized_weights, r, nsim);
    
    arma::mat alphatmp(m, nsim);
    
    // for (unsigned int i = 0; i < nsim; i++) {
    //   alphatmp.col(i) = alpha.slice(i).col(t);
    // }
    for (unsigned int i = 0; i < nsim; i++) {
      alphatmp.col(i) = alpha.slice(indices(i, t)).col(t);
      //alpha.slice(i).col(t) = alphatmp.col(indices(i, t));
    }
    for (unsigned int i = 0; i < nsim; i++) {
      arma::vec um(m);
      for(unsigned int j = 0; j < m; j++) {
        um(j) = normal(engine);
      }
      alpha.slice(i).col(t + 1) = alphahat.col(t + 1) +
        Ct.slice(t + 1) * (alphatmp.col(i) - alphahat.col(t)) + Vt.slice(t + 1) * um;
    }
    
    if ((t < (n - 1)) && arma::is_finite(y(t + 1))) {
      weights.col(t + 1) =
        arma::exp(log_weights(approx_model, t + 1, alpha) - scales(t + 1));
      double sum_weights = arma::accu(weights.col(t + 1));
      if(sum_weights > 0.0){
        normalized_weights = weights.col(t + 1) / sum_weights;
      } else {
        return -std::numeric_limits<double>::infinity();
      }
      loglik += std::log(sum_weights / nsim);
    } else {
      weights.col(t + 1).ones();
      normalized_weights.fill(1.0 / nsim);
    }
  }
  return loglik;
}
int calculate_method::PWI_Compute(string input,string output,PWIConstTable *PWIconst)
{
    int S0Index;
    //PWI read
    DicomInfo PWIinfo;
     cout<<"after sort"<<endl;
    getDcm *PWIcommon=new getDcm(input);
    //读取PWI原始信息
    for(int i = 0 ;i < PWIconst->PSetNum ; i++)
    {
        for(int j = 0 ; j < PWIconst->PSliceNum ;j++)
        {
            PWIinfo.getInfo((PWIcommon->dir+"IM"+itos(i)+"-"+itos(j)).c_str());
            PWIArray[TWO_D(i,j,PWIconst->PSliceNum)] = PWIinfo;
        }
    }
    PWIArray[TWO_D(0,0,PWIconst->PSetNum)].dataset->findAndGetUint16(DcmTagKey(0x0028,0x0010),PWIcommon->height); //读取图片高度
    PWIArray[TWO_D(0,0,PWIconst->PSetNum)].dataset->findAndGetUint16(DcmTagKey(0x0028,0x0011),PWIcommon->width);  //读取图片宽度
    DicomInfo *s0 = new DicomInfo[PWIconst->PSliceNum];
    for(int i = 0; i < PWIconst->PSliceNum; ++i)
    {
        s0[i].allocMemory( PWIcommon->height * PWIcommon->width);
    }

    S0Index =  PWIconst->STDNum-1;
    //获取S0值
    for(int i = 0;i < PWIconst->PSliceNum ; ++i)
    {
        for(int j = 0;j < PWIcommon->height; ++j)
        {
            for(int k = 0;k < PWIcommon->width;++k)
            {
                s0[i].PixelData[TWO_D(j,k,PWIcommon->width)] = PWIArray[TWO_D(S0Index,i,PWIconst->PSliceNum)].PixelData[TWO_D(j,k,PWIcommon->width)];
            }
        }
    }



    //获取s0的均值
    int signalnum,s0Sum = 0;
    _signal avg=0;
    for(int i = 0;i < PWIconst->PSliceNum ; ++i )
    {
        signalnum=0;
        s0Sum = 0;
        for(int j = 0;j < PWIcommon->height; ++j)
        {
            for(int k = 0 ; k < PWIcommon->width; ++k)
            {
                if(s0[i].PixelData[TWO_D(j,k,PWIcommon->width)] != 0)
                {
                    s0Sum += PWIArray[TWO_D(S0Index,i,PWIconst->PSliceNum)].PixelData[TWO_D(j,k,PWIcommon->width)];
                    signalnum++;
                }
            }
        }
        avg = ((_signal)s0Sum)/signalnum * 0.5;
        //过滤掉小于50%的点   mask ADC 600-2100!!!!!
        for(int k = 0;k < PWIcommon->height; ++k)
            {
                for(int l = 0;l < PWIcommon->width; ++l)
                {
                    if(s0[i].PixelData[TWO_D(k,l,PWIcommon->width)]<avg)
                    {
                        s0[i].PixelData[TWO_D(k,l,PWIcommon->width)] = 0 ;
                    }
                }
            }

    }
    //计算R2
    for(int i = 0;i < PWIconst->PSetNum ; ++i)
    {
        for(int j = 0;j < PWIconst->PSliceNum; ++j)
        {
            for(int k = 0;k < PWIcommon->height; ++k)
            {
                for(int l = 0;l < PWIcommon->width; ++l)
                {
                    //判断是否为过滤掉的点
                    if(s0[j].PixelData[TWO_D(k,l,PWIcommon->width)] != 0 )
                    {
                        if(PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]!=0)
                        {
                            PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]=R2(s0[j].PixelData[TWO_D(k,l,PWIcommon->width)],PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)],PWIconst->TE);
                        }else
                        {
                            //当St值为0时的处理方法
                            PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]=R2(s0[j].PixelData[TWO_D(k,l,PWIcommon->width)],1,PWIconst->TE);//测试第二个参数!!!!
                        }
                    }
                    else
                    {
                        //将过滤掉的点置为0
                        PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)] = 0;
                    }


                }
            }

        }
    }
    //get Position of AIF

    for(int i = 0;i < PWIconst->PSetNum ; ++i)//分配内存
    {
        for(int j = 0;j <  PWIconst->PSliceNum; ++j)
        {

            PWICav[TWO_D(i,j,PWIconst->PSliceNum)].allocMemory(PWIcommon->height*PWIcommon->width);//分配空间
            PWICt[TWO_D(i,j,PWIconst->PSliceNum)].allocMemory(PWIcommon->height*PWIcommon->width);
            PWIRt[TWO_D(i,j,PWIconst->PSliceNum)].allocMemory(PWIcommon->height*PWIcommon->width);
            PWICav[TWO_D(i,j,PWIconst->PSliceNum)].time=PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].time;
        }
    }
    int MRtype =1;// PWIconst->TType;//MRI类型 1.5T 或者 3.0T
    for(int i = 0 ;i < PWIconst->PSetNum; ++i)
    {
        for(int j = 0;j < PWIconst->PSliceNum;++j)
        {
            for(int k = 0;k < PWIcommon->height;++k)
            {
                for(int l = 0;l < PWIcommon->width;++l)
                {

                       PWICt[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]=  Ct(PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)],MRtype,PWIconst->x[MRtype][0]);
                }
            }
        }
    }



    //AIF Selection
    //get avg of Area
    DicomInfo * AreaAndCt = new DicomInfo[ PWIconst->PSliceNum ];
    for(int j = 0 ; j < PWIconst->PSliceNum ; ++j)
    {
        AreaAndCt[j].allocMemory(PWIcommon->height*PWIcommon->width);
        for(int i =0 ; i < PWIcommon->height*PWIcommon->width ; ++i)
        {
            AreaAndCt[j].PixelData[i] = 0;
        }
    }
    double temArea = 0;
    int AllCurve = 0;
    _signal Area;
    _signal SecondOrderThreshold;
    int AIFtestZ;
    int AIFtestX;
    int AIFtestY;
    double testC = 0;
    double temC = 0;
    vector<double> AreaSet;
    _signal * DsFirstOrder = new _signal [PWIconst->PSetNum];
    _signal * DsSecondOrder = new _signal [PWIconst->PSetNum];
    for(int j = 10;j < 20; ++j)
    {
        for(int k = 0;k < PWIcommon->height*2/3; ++k)
            {

                for(int l = 0;l < PWIcommon->width; ++l)
                {
                    if(s0[j].PixelData[TWO_D(k,l,PWIcommon->width)]==0)
                    {
                        continue;
                    }
                    AllCurve++;
                    for(int i = PWIconst->STDNum-1;i < PWIconst->PSetNum-1 ; ++i)
                    {
                        AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)] += PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]+PWIArray[TWO_D(i+1,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)];
                    }
                    AreaSet.push_back(AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)]);
                }
            }
    }
    sort(AreaSet.begin(),AreaSet.end());
    Area = AreaSet[(int)(AllCurve*0.9)];
    AreaSet.clear();
    //Ct daoshu
    AllCurve = 0;
    for(int j = 10;j < 20; ++j)
    {
        for(int k = 0;k < PWIcommon->height*2/3; ++k)
            {

                for(int l = 0;l < PWIcommon->width; ++l)
                {
                    if(s0[j].PixelData[TWO_D(k,l,PWIcommon->width)]==0||AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)]<Area)
                    {
                        AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)] = 0;
                        continue;
                    }
                    ++AllCurve;
                    for(int i = PWIconst->STDNum-1; i< PWIconst->PSetNum ; ++i)
                    {
                        DsSecondOrder[i] = PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)];
                    }
                    for(int i = PWIconst->STDNum-1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(i==PWIconst->STDNum-1)
                        {
                            DsFirstOrder[i] = (-3*DsSecondOrder[i] + 4 * DsSecondOrder[i+1] - DsSecondOrder[i+2]);

                        }else if(i == PWIconst->PSetNum-1)
                        {
                            DsFirstOrder[i] = (DsSecondOrder[i-2] - 4 * DsSecondOrder[i-1] + 3 * DsSecondOrder[i]);

                        }else
                        {
                            DsFirstOrder[i] = ( -1*DsSecondOrder[i-1]+DsSecondOrder[i+1]);

                        }
                    }
                    for(int i = PWIconst->STDNum-1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(i==PWIconst->STDNum-1)
                        {
                            DsSecondOrder[i] = (-3*DsFirstOrder[i] + 4 * DsFirstOrder[i+1] - DsFirstOrder[i+2]);

                        }else if(i == PWIconst->PSetNum-1)
                        {
                            DsSecondOrder[i] = (DsFirstOrder[i-2] - 4 * DsFirstOrder[i-1] + 3 * DsFirstOrder[i]);

                        }else
                        {
                            DsSecondOrder[i] = ( -1*DsFirstOrder[i-1]+DsFirstOrder[i+1]);

                        }
                        DsSecondOrder[i]=DsSecondOrder[i]*DsSecondOrder[i];
                    }
                    _signal temSecondOrder = calcul(1.8,DsSecondOrder+PWIconst->STDNum-1,PWIconst->PSetNum-PWIconst->STDNum);
                    AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)] = temSecondOrder;
                    AreaSet.push_back(temSecondOrder);

                }
            }
    }
    sort(AreaSet.begin(),AreaSet.end());
    SecondOrderThreshold = AreaSet[(int)(AllCurve*0.75)];
    AreaSet.clear();
    for(int j = 10;j < 20; ++j)
    {
        for(int k = 0;k < PWIcommon->height*2/3; ++k)
            {

                for(int l = 0;l < PWIcommon->width; ++l)
                {
                    if(AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)]>SecondOrderThreshold)
                    {
                        AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)] = 0;
                    }
                }
        }
    }
    /**SecondOrder version**/
    double avgTime,avgHeight,avgWidth;
    avgTime = 0;
    avgHeight = 0;
    avgWidth = 0;
    double minIndex = 0;
    double maxIndex = 0;
    double maxR2 = -100;
    for(int j = 10;j < 20; ++j)
    {
        for(int k = 0;k < PWIcommon->height*2/3; ++k)
        {

                for(int l = 0;l < PWIcommon->width; ++l)
                {
                    minIndex = PWIconst->PSetNum-1;
                    maxIndex = -1;
                    maxR2 = -100;
                    if(AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)] == 0)
                    {
                        continue;
                    }
                    for(int i = PWIconst->STDNum-1; i< PWIconst->PSetNum ; ++i)
                    {

                        DsSecondOrder[i] = PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)];
                        if(maxR2<DsSecondOrder[i])
                            maxR2 = DsSecondOrder[i];
                    }
                    for(int i = PWIconst->STDNum-1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(i==PWIconst->STDNum-1)
                        {
                            DsFirstOrder[i] = (-3*DsSecondOrder[i] + 4 * DsSecondOrder[i+1] - DsSecondOrder[i+2]);

                        }else if(i == PWIconst->PSetNum-1)
                        {
                            DsFirstOrder[i] = (DsSecondOrder[i-2] - 4 * DsSecondOrder[i-1] + 3 * DsSecondOrder[i]);

                        }else
                        {
                            DsFirstOrder[i] = ( -1*DsSecondOrder[i-1]+DsSecondOrder[i+1]);

                        }
                    }
                    for(int i = PWIconst->STDNum+1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(i==PWIconst->STDNum-1)
                        {
                            DsSecondOrder[i] = (-3*DsFirstOrder[i] + 4 * DsFirstOrder[i+1] - DsFirstOrder[i+2]);

                        }else if(i == PWIconst->PSetNum-1)
                        {
                            DsSecondOrder[i] = (DsFirstOrder[i-2] - 4 * DsFirstOrder[i-1] + 3 * DsFirstOrder[i]);

                        }else
                        {
                            DsSecondOrder[i] = ( -1*DsFirstOrder[i-1]+DsFirstOrder[i+1]);

                        }
                    }
                    for(int i = PWIconst->STDNum+1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(DsSecondOrder[i] >0.008)
                        {
                            if(i<minIndex)
                            {
                                minIndex = i;
                            }
                            if(i>maxIndex)
                            {
                                maxIndex =i;
                            }
                        }
                    }
                    if(maxIndex == -1)
                    {
                        maxIndex =PWIconst->PSetNum-1;
                    }
                    if(minIndex == PWIconst->PSetNum-1)
                    {
                        minIndex =0;
                        maxIndex = PWIconst->PSetNum-1;
                    }
                    avgHeight += maxR2 * 100;
                    avgTime += minIndex* 1.8;
                    avgWidth += (maxIndex - minIndex)*1.8;

                }
        }
    }
    avgHeight/=AllCurve;
    avgWidth/=AllCurve;
    avgTime/=AllCurve;
    for(int j = 10;j < 20; ++j)
    {
        for(int k = 0;k < PWIcommon->height*2/3; ++k)
        {

                for(int l = 0;l < PWIcommon->width; ++l)
                {
                    minIndex = PWIconst->PSetNum-1;
                    maxIndex = -1;
                    maxR2 = -100;
                    if(AreaAndCt[j].PixelData[TWO_D(k,l,PWIcommon->width)] == 0)
                    {
                        continue;
                    }
                    for(int i = PWIconst->STDNum-1; i< PWIconst->PSetNum ; ++i)
                    {

                        DsSecondOrder[i] = PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)];
                        if(maxR2<DsSecondOrder[i])
                            maxR2 = DsSecondOrder[i];
                    }
                    for(int i = PWIconst->STDNum-1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(i==PWIconst->STDNum-1)
                        {
                            DsFirstOrder[i] = (-3*DsSecondOrder[i] + 4 * DsSecondOrder[i+1] - DsSecondOrder[i+2]);

                        }else if(i == PWIconst->PSetNum-1)
                        {
                            DsFirstOrder[i] = (DsSecondOrder[i-2] - 4 * DsSecondOrder[i-1] + 3 * DsSecondOrder[i]);

                        }else
                        {
                            DsFirstOrder[i] = ( -1*DsSecondOrder[i-1]+DsSecondOrder[i+1]);

                        }
                    }
                    for(int i = PWIconst->STDNum+1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(i==PWIconst->STDNum-1)
                        {
                            DsSecondOrder[i] = (-3*DsFirstOrder[i] + 4 * DsFirstOrder[i+1] - DsFirstOrder[i+2]);

                        }else if(i == PWIconst->PSetNum-1)
                        {
                            DsSecondOrder[i] = (DsFirstOrder[i-2] - 4 * DsFirstOrder[i-1] + 3 * DsFirstOrder[i]);

                        }else
                        {
                            DsSecondOrder[i] = ( -1*DsFirstOrder[i-1]+DsFirstOrder[i+1]);

                        }
                    }
                    for(int i = PWIconst->STDNum+1;i < PWIconst->PSetNum ; ++i)
                    {
                        if(DsSecondOrder[i] >0.008)
                        {
                            if(i<minIndex)
                            {
                                minIndex = i;
                            }
                            if(i>maxIndex)
                                maxIndex =i;
                        }
                    }
                    if(maxIndex == -1)
                    {
                        maxIndex =PWIconst->PSetNum-1;
                    }
                    if(minIndex == PWIconst->PSetNum-1)
                    {
                        minIndex =0;
                        maxIndex = PWIconst->PSetNum-1;
                    }
                    if(minIndex == maxIndex)
                        maxIndex = PWIconst->PSetNum-1;

                        temC = PWIconst->ck1*(maxR2*100 - avgHeight) + PWIconst->ck2*(minIndex*1.8 - avgTime) + PWIconst->ck3 * ((maxIndex - minIndex)*1.8 - avgWidth);
                        if(maxR2*100>avgHeight && minIndex*1.8<avgTime && (maxIndex - minIndex)*1.8<avgWidth)
                        {
                            if(temC > testC)
                            {
                                testC = temC;
                                AIFtestZ = j;
                                AIFtestX = l;
                                AIFtestY = k;
                            }
                        }

                }
        }
    }
    delete  [] AreaAndCt;
    delete  [] DsFirstOrder;
    delete  [] DsSecondOrder;
    int AIFz = AIFtestZ;
    int AIFy = AIFtestY;
    int AIFx = AIFtestX;

    /*AIF end*/



    for(int i = 0 ;i < PWIconst->PSetNum; ++i)
    {
        for(int j = 0;j < PWIconst->PSliceNum;++j)
        {
            for(int k = 0;k < PWIcommon->height;++k)
            {
                for(int l = 0;l < PWIcommon->width;++l)
                {
                    //获取Cav的值
                        PWICav[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]=Cav(PWIArray[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)],MRtype,PWIconst->x[MRtype][1],PWIconst->x[MRtype][2]);

                }
            }
        }
    }
    Uint16 temPixel[128*128];
       cout<<"Cav Ct OK"<<endl;
    //给FFT输入做赋值
    fftw_plan p;
    //计算卷积
    for(int i = 0 ;i < PWIconst->PSliceNum; ++i )
    {

            for(int j = 0; j < PWIcommon->height;++j)
            {
                for(int k = 0;k < PWIcommon->width; ++k)
                {
                    //zero padding!!!!!!!!!!!!!! 不是这样
                    for(int l = S0Index ; l < PWIconst->PSetNum;++l)
                    {   //15 52 44
                        TempInFFTCt[l-S0Index][0]=PWICt[TWO_D(l,i,PWIconst->PSliceNum)].PixelData[TWO_D(j,k,PWIcommon->width)];
                        TempInFFTCa[l-S0Index][0]=PWICav[TWO_D(l,AIFz,PWIconst->PSliceNum)].PixelData[TWO_D(AIFy,AIFx,PWIcommon->width)];
                        TempInFFTCt[l-S0Index][1] = 0;
                        TempInFFTCa[l-S0Index][1] = 0;
                    }
                    for(int l = PWIconst->PSetNum-S0Index ; l < 2*(PWIconst->PSetNum-S0Index);++l)
                    {
                        TempInFFTCt[l][0] = 0;
                        TempInFFTCa[l][0] = 0;
                        TempInFFTCt[l][1] = 0;
                        TempInFFTCa[l][1] = 0;
                    }
                    //计算FFT
                    p=fftw_plan_dft_1d( PWIconst->PSetNum-S0Index ,TempInFFTCt ,TempOutFFTCt , FFTW_FORWARD, FFTW_ESTIMATE);
                    fftw_execute(p);
                    fftw_destroy_plan(p);
                    p=fftw_plan_dft_1d(PWIconst->PSetNum-S0Index ,TempInFFTCa, TempOutFFTCa , FFTW_FORWARD, FFTW_ESTIMATE);
                    fftw_execute(p); //理解out
                    fftw_destroy_plan(p);
                    //FFT(TempInFFTCt,TempOutFFTCt,FFTLayer);
                    //FFT(TempInFFTCa,TempOutFFTCa,FFTLayer);
                    _signal maxFFTCa=-99999;
                    for(int l = 0 ; l < 2 * (PWIconst->PSetNum - S0Index); ++l)
                    {
                        int tempFFTCa = sqrt(TempOutFFTCa[l][0]*TempOutFFTCa[l][0]+TempOutFFTCa[l][1]*TempOutFFTCa[l][1]);
                        if(maxFFTCa < tempFFTCa)
                            maxFFTCa = tempFFTCa;
                    }

                    _signal TempN = maxFFTCa * PWIconst->pr/2;
                    _signal TempG;
                    for(int l = 0 ; l < 2 * (PWIconst->PSetNum - S0Index); ++l)
                    {
                        int tempFFTCa = sqrt(TempOutFFTCa[l][0]*TempOutFFTCa[l][0]+TempOutFFTCa[l][1]*TempOutFFTCa[l][1]);
                        if(tempFFTCa > TempN && l != 0)
                        {
                            TempG=(tempFFTCa*tempFFTCa-TempN*TempN)/(tempFFTCa*tempFFTCa);
                        }else if(l==0)
                        {
                            TempG=1;
                        }else
                        {
                            TempG=0;
                        }
                        if(TempG!=0)
                        {
                            TempInIFFT[l][0]=TempG * Divide_Real(TempOutFFTCt[l],TempOutFFTCa[l]);
                            TempInIFFT[l][1]=TempG * Divide_Img(TempOutFFTCt[l],TempOutFFTCa[l]);
                        }else
                        {
                            TempInIFFT[l][0]=0;
                            TempInIFFT[l][1]=0;
                        }
                        //cout<<TempInIFFT[l]<<endl;

                    }
                    //计算IFFT
                    p=fftw_plan_dft_1d(PWIconst->PSetNum-S0Index,TempInIFFT,TempOutIFFT, FFTW_BACKWARD, FFTW_ESTIMATE);
                    fftw_execute(p);
                    fftw_destroy_plan(p);

                    //求出Rt
                    for(int l = 0 ; l < PWIconst->PSetNum-S0Index; ++l)
                    {
                        PWIRt[TWO_D(l,i,PWIconst->PSliceNum)].PixelData[TWO_D(j,k,PWIcommon->width)]=1/(PWIconst->PSetNum*1.8)*TempOutIFFT[l][0];
                        //cout<<PWIRt[TWO_D(i,l,PWIconst->PSliceNum)].PixelData[TWO_D(j,k,PWIcommon->width)]<<endl;
                    }

                }

            }
    }

    //free fft



    //估算Kav
    int calVOF;
    int calAIF;
    _signal temVOF[62];
    _signal temAIF[62];
    calVOF = calcul(1800,temVOF,PWIconst->PSetNum);
    calAIF = calcul(1800,temAIF,PWIconst->PSetNum);

    cout<<"KAV:   AIF:"<<calAIF<<"   VOF:"<<calVOF<<endl;

    //计算CBV MTT
    for(int i = 0; i < PWIconst->PSliceNum; ++i)
    {
        PWICBV[i].allocMemory(PWIcommon->height*PWIcommon->width);
        PWIMTT[i].allocMemory(PWIcommon->height*PWIcommon->width);
        PWICBF[i].allocMemory(PWIcommon->height*PWIcommon->width);
        PWITmax[i].allocMemory(PWIcommon->height*PWIcommon->width);

    }

    _signal Integral_Ca=0;
    _signal Integral_Ct=0;
    _signal k0 = getK0(PWIconst->ro,PWIconst->HSV,PWIconst->HLV);
    int kav =1;

        for(int j = 0 ; j < PWIconst->PSliceNum; ++j)
        {
            for(int k = 0; k < PWIcommon->height; ++k)
            {
                for(int l = 0; l < PWIcommon->width; ++l)
                {
                    _signal maxRt = -10000;
                    _signal maxRtt = 0;
                    for(int i = 0; i < PWIconst->PSetNum-S0Index; ++i)
                    {
                        tempCt[i]=PWICt[TWO_D(i+S0Index,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)];
                        tempCa[i]=PWICav[TWO_D(i+S0Index,AIFz,PWIconst->PSliceNum)].PixelData[TWO_D(AIFy,AIFx,PWIcommon->width)];
                        //求出rt达到最大值时的t值
                        /*if(j==SelectSlice&&k==index_x&&l==index_y)
                        {
                            cout<<PWIRt[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]<<" ";
                        }*/
                        //i<=(PWIconst->PSetNum-S0Index)/2+1 &&
                        if( i <= (PWIconst->PSetNum-S0Index)/2+1 && PWIRt[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)]>maxRt)
                        {
                            maxRt = PWIRt[TWO_D(i,j,PWIconst->PSliceNum)].PixelData[TWO_D(k,l,PWIcommon->width)];
                            maxRtt = i*1.8;

                        }
                    }

                    Integral_Ca=calcul(1800,tempCa,PWIconst->PSetNum-S0Index);
                    Integral_Ct=calcul(1800,tempCt,PWIconst->PSetNum-S0Index);
                    //if(s0[j].PixelData[TWO_D(k,l,PWIcommon->width)] != 0)//是否在mask内
                        PWICBV[j].PixelData[TWO_D(k,l,PWIcommon->width)]=Integral_Ct/Integral_Ca*k0*kav;
                    //else
                    //	PWICBV[j].PixelData[TWO_D(k,l,PWIcommon->width)] = 0;


                    PWICBF[j].PixelData[TWO_D(k,l,PWIcommon->width)]= maxRt*k0*kav*60;
                    PWIMTT[j].PixelData[TWO_D(k,l,PWIcommon->width)] = maxRtt;

                    /*if(j==SelectSlice&&l==index_x&&k==index_y)
                    {
                            cout<<"MaxRt:"<<maxRt<<" PWICBF:"<<PWICBF[j].PixelData[TWO_D(k,l,PWIcommon->width)]<<"  shu:"<<maxRt*k0*kav*60<<endl;
                    }*/

                }


            }

            string s_mtt_tem = output+"bmpMTT/"+itos(j)+"_MTT.bmp";
            string s_tmax_tem = output+"bmpTmax/"+itos(j)+"_Tmax.bmp";
            string s_cbf_tem = output+"bmpCBF/"+itos(j)+"_CBF.bmp";
            string s_cbv_tem = output+"bmpCBV/"+itos(j)+"_CBV.bmp";
            //计算MTT
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                PWITmax[j].PixelData[i] = 60*PWICBV[j].PixelData[i]/PWICBF[j].PixelData[i];
            }
            //输出MTT
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)(PWITmax[j].PixelData[i]*10);
            }
            PWIinfo.fileformat.getDataset()->putAndInsertUint16Array(DCM_PixelData,temPixel,128*128);
            PWIinfo.fileformat.saveFile((output+"Tmax/MTT_"+itos(j)).c_str(),EXS_LittleEndianExplicit);

            //输出Tmax
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)(PWIMTT[j].PixelData[i]);
            }
            PWIinfo.fileformat.getDataset()->putAndInsertUint16Array(DCM_PixelData,temPixel,128*128);
            PWIinfo.fileformat.saveFile((output+"MTT/MTT_"+itos(j+1)).c_str(),EXS_LittleEndianExplicit);
            //输出CBF
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)(PWICBF[j].PixelData[i]*10);
            }
            PWIinfo.fileformat.getDataset()->putAndInsertUint16Array(DCM_PixelData,temPixel,128*128);
            PWIinfo.fileformat.saveFile((output+"CBF/CBF_"+itos(j)).c_str(),EXS_LittleEndianExplicit);
                //输出CBV
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)(PWICBV[j].PixelData[i]*100);
            }
            PWIinfo.fileformat.getDataset()->putAndInsertUint16Array(DCM_PixelData,temPixel,128*128);
            PWIinfo.fileformat.saveFile((output+"CBV/CBV_"+itos(j)).c_str(),EXS_LittleEndianExplicit);
            //write BMP
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)((PWICBV[j].PixelData[i]<=0?0:PWICBV[j].PixelData[i])*100);
            }
            toBmp(128,128,temPixel,s_cbv_tem.c_str(),0,500,s0[j].PixelData);
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)((PWICBF[j].PixelData[i]<=0?0:PWICBF[j].PixelData[i])*10);
            }
            toBmp(128,128,temPixel,s_cbf_tem.c_str(),0,400,s0[j].PixelData);
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)((PWIMTT[j].PixelData[i]<=0?0:PWIMTT[j].PixelData[i])*10);
            }

            toBmp(128,128,temPixel,s_tmax_tem.c_str(),0,120,s0[j].PixelData);
            for(int i = 0 ; i < 128*128 ; ++i )
            {
                temPixel[i] = (Uint16)((PWITmax[j].PixelData[i]<=0?0:PWITmax[j].PixelData[i])*10);
            }
            toBmp(128,128,temPixel,s_mtt_tem.c_str(),0,160,s0[j].PixelData);
        }
    puts("PWI DONE");
    return 0;
}