matlab中sqrt_design函数的C语言实现

% matlab中的sqrt_design函数代码
function b = sqrt_design(n,fc,fs,R)

ind1 = find(n == 0);
if ~isempty(ind1),
	b(ind1) = - sqrt(2.*fc) ./ (pi.*fs) .* (pi.*(R-1) - 4.*R );
end

ind2 = find(abs(abs(8.*R.*fc.*n) - 1.0) < sqrt(eps));
if ~isempty(ind2),
	b(ind2) = sqrt(2.*fc) ./ (2.*pi.*fs) ...
		* (    pi.*(R+1)  .* sin(pi.*(R+1)./(4.*R)) ...
		- 4.*R     .* sin(pi.*(R-1)./(4.*R)) ...
		+ pi.*(R-1)  .* cos(pi.*(R-1)./(4.*R)) ...
		);
end

ind = 1:length(n);
ind([ind1 ind2]) = [];
nind = n(ind);

b(ind) = -4.*R./fs .* ( cos((1+R).*2.*pi.*fc.*nind) + ...
	sin((1-R).*2.*pi.*fc.*nind) ./ (8.*R.*fc.*nind) ) ...
	./ (pi .* sqrt(1./(2.*fc)) .* ((8.*R.*fc.*nind).^2 - 1));

b = sqrt(2.*fc) .* b;

sqrt_design函数的C语言实现:

#include
#include
#include

#define pi 4*atan(1)
#define eps 2.2204e-16


typedef struct list_float
{
     
	float *vector;
	int len_vector;
}list_float,*plist_float;

typedef struct list_double
{
     
	//没用到
	double *vector;
	int len_vector;
}list_double, *plist_double;

typedef struct list_int
{
     
	int *vector;
	int len_vector;
}list_int, *plist_int;

void show(plist_int input)
{
     
	int j = 0;
	for (j = 0; j < input->len_vector; ++j)
	{
     
		printf("%dvalue = %d\n", j,input->vector[j]);
	}
}
void show_f(plist_float input)
{
     
	int j = 0;
	/*
	for (j = 0; j < input->len_vector; ++j)
	{
		printf("%dvalue = %.10f\n", j, input->vector[j]);
	}
	*/
	for (j = 0; j < input->len_vector; ++j)
	{
     
		printf("%d:%.10f\n ", j, input->vector[j]);
	}
}

plist_float init_list_float(int len_Array)
{
     
	plist_float pfloatlist = (plist_float)malloc(sizeof(list_float));
	pfloatlist->vector = (float *)malloc(sizeof(float)*len_Array);;
	pfloatlist->len_vector = len_Array;
	return pfloatlist;
}

plist_float value_list_float(float * Array, int len_Array)
{
     
	plist_float pfloatlist = (plist_float)malloc(sizeof(list_float));
	pfloatlist->vector = Array;
	pfloatlist->len_vector = len_Array;
	return pfloatlist;
}

plist_int init_list_int(int len_Array)
{
     
	plist_int pfloatlist = (plist_int)malloc(sizeof(plist_int));
	pfloatlist->vector = (int *)malloc(sizeof(int)*len_Array);
	pfloatlist->len_vector = len_Array;
	return pfloatlist;
}

plist_int find(plist_float plistfloat, float value, int tager)
{
     
	//返回列表下标
	//tager == 0 plistfloat==value
	//tager == 1 plistfloat>value
	//tager == -1 plistfloat
	int *index = (int *)malloc(sizeof(int));
	int len_index = 0;
	int j = 0;
	switch (tager)
	{
     
		case 0:
		{
     
			for (j = 0; j < plistfloat->len_vector; ++j)
			{
     
				if (fabs(plistfloat->vector[j] - value) < 1e-6)
				{
     
					len_index++;
					index = (int *)realloc(index, sizeof(int)*len_index);
					index[len_index - 1] = j;
				}
			}
			break;
		}
		case 1:
		{
     
			for (j = 0; j < plistfloat->len_vector; ++j)
			{
     
				if (plistfloat->vector[j] > value)
				{
     
					len_index++;
					index = (int *)realloc(index, sizeof(int)*len_index);
					index[len_index - 1] = j;
				}
			}
			break;
		}
		case -1:
		{
     
			for (j = 0; j < plistfloat->len_vector; ++j)
			{
     
				if (plistfloat->vector[j] < value)
				{
     
					len_index++;
					index = (int *)realloc(index, sizeof(int)*len_index);
					index[len_index - 1] = j;
				}
			}
			break;
		}
		default:
			break;
	}
	if (len_index == 0)
	{
     
		return NULL;
	}
	else
	{
     
		plist_int indexlist = (plist_int)malloc(sizeof(list_int));
		indexlist->len_vector = len_index;
		indexlist->vector = index;
		return indexlist;
	}
}


int isempty(plist_float plistfloat)
{
     
	if (plistfloat->len_vector == 0)
	{
     
		return 1;
	}
	else
	{
     
		return 0;
	}
}

plist_float lfabs(plist_float plistfloat)
{
     
	//列表的绝对值,不对原列表操作,返回新列表。
	int i = 0;
	plist_float pf = init_list_float(plistfloat->len_vector);
	for (i = 0;i<plistfloat->len_vector;++i)
	{
     
		pf->vector[i] = fabs(plistfloat->vector[i]);
	}
	return pf;
}

plist_float VdotList(float v, plist_float plistfloat)
{
     
	//列表乘以一个值V,不对原列表操作,返回新列表。
	int i = 0;
	plist_float pf = init_list_float(plistfloat->len_vector);
	for (i = 0;i<plistfloat->len_vector;++i)
	{
     
		pf->vector[i] = v * plistfloat->vector[i];
	}
	return pf;
}

plist_float List_V(float v, plist_float plistfloat)
{
     
	// 列表减掉一个值V,不对原列表操作,返回新列表。
	int i = 0;
	for (i = 0; i<plistfloat->len_vector; ++i)
	{
     
		plistfloat->vector[i] = plistfloat->vector[i] - v;
	}
	return plistfloat;
}

int intVinList(int v, plist_int plistint)//效率低下
{
     
	int i = 0;
	for (i = 0; i < plistint->len_vector; ++i)
	{
     
		if (v == plistint->vector[i])
		{
     
			return 1;
		}
	}
	return 0;
}

plist_float sqrt_design(plist_float n,float fc,float fs,float R)
{
     
	plist_float b = init_list_float(0);
	plist_int ind1 = find(n, 0, 0);
	plist_int ind2 = find(lfabs(List_V(1.0,lfabs(VdotList(8.0*R*fc,n)))), sqrt(eps),-1);
	plist_int ind = init_list_int(n->len_vector - ind1->len_vector - ind2->len_vector);
	plist_float nind = init_list_float(ind->len_vector);
	int *p = ind->vector;//指针p指向ind的列表
	int i = 0;
	//show(ind2);
	//**** 从这里开始,按照matlab的sqrt_design函数编写C语言代码
	if (ind1->len_vector != 0)
	{
     
		b->len_vector = ind1->vector[ind1->len_vector - 1] + 1;
		b->vector = (float *)realloc(b->vector,sizeof(float)*(b->len_vector));
		for (i = 0; i < b->len_vector; ++i)
		{
     
			b->vector[i] = 0.0;
		}
		for (i = 0; i < ind1->len_vector; ++i)
		{
     
			b->vector[ind1->vector[i]] = -1.0*sqrt(2.0*fc)/(pi*fs) * (pi*(R - 1.0) - 4.0*R);
		}
	}
	
	if (ind2->len_vector != 0)
	{
     
		if (ind2->vector[ind2->len_vector - 1] + 1 > b->len_vector)
		{
     
			b->len_vector = ind2->vector[ind2->len_vector - 1] + 1;
			b->vector = (float *)realloc(b->vector, sizeof(float)*(b->len_vector));
		}
		for (i = 0; i < ind2->len_vector; ++i)
		{
     
			b->vector[ind2->vector[i]] = sqrt(2.*fc) / (2.*pi*fs)* (pi*(R + 1)*sin(pi*(R + 1) / (4.*R))- 4.*R*sin(pi*(R - 1)/ (4.*R))+ pi*(R - 1)*cos(pi*(R - 1)/ (4.*R)));
		}
	}
	//show_f(b);
	for (i = 0; i < n->len_vector;i++)
	{
     
		//对ind的列表进行赋值
		if (intVinList(i, ind1) == 0 && intVinList(i, ind2) == 0)
		{
     
			*p = i;
			++p;
		}
	}
	
	for (i = 0; i < nind->len_vector; ++i)
	{
     
		nind->vector[i] = n->vector[ind->vector[i]];
	}
	
	show_f(nind);
	//i = 0;
	b->len_vector = ind->vector[ind->len_vector - 1] + 1;
	b->vector = (float *)realloc(b->vector, sizeof(float)*(b->len_vector));
	//b->vector[ind->vector[i]] = -4 * R / fs* (cos((1 + R)*2.*pi*fc*(nind->vector[ind->vector[i]])) + sin((1 - R)*2.*pi*fc*(nind->vector[ind->vector[i]])) / (8 * R*fc*(nind->vector[ind->vector[i]]))) / (pi*sqrt(1 / (2 * fc)) * ((8.0*R*fc*(nind->vector[ind->vector[i]]))*(8.0*R*fc*(nind->vector[ind->vector[i]])) - 1));
	for (i = 0; i < ind->len_vector; ++i)
	{
     
		//注意对应下标,zz
		b->vector[ind->vector[i]] = -4*R/fs* (cos((1 + R)*2.*pi*fc*(nind->vector[i])) + sin((1 - R)*2.*pi*fc*(nind->vector[i]))/(8*R*fc*(nind->vector[i])))/(pi*sqrt(1/(2*fc)) * ((8.0*R*fc*(nind->vector[i]))*(8.0*R*fc*(nind->vector[i])) - 1));
	}
	//***** 到这里结束
	return b;
}





int main()
{
      
	float a[10] = {
      0.0,0.23,0.69,0.0,3.69,5.48,9.3214,0.2218,0.00001,0.368 };
	float n_[161] = {
      -5,-4.93750000000000,-4.87500000000000,-4.81250000000000,-4.75000000000000,-4.68750000000000,-4.62500000000000,-4.56250000000000,-4.50000000000000,-4.43750000000000,-4.37500000000000,-4.31250000000000,-4.25000000000000,-4.18750000000000,-4.12500000000000,-4.06250000000000,-4,-3.93750000000000,-3.87500000000000,-3.81250000000000,-3.75000000000000,-3.68750000000000,-3.62500000000000,-3.56250000000000,-3.50000000000000,-3.43750000000000,-3.37500000000000,-3.31250000000000,-3.25000000000000,-3.18750000000000,-3.12500000000000,-3.06250000000000,-3,-2.93750000000000,-2.87500000000000,-2.81250000000000,-2.75000000000000,-2.68750000000000,-2.62500000000000,-2.56250000000000,-2.50000000000000,-2.43750000000000,-2.37500000000000,-2.31250000000000,-2.25000000000000,-2.18750000000000,-2.12500000000000,-2.06250000000000,-2,-1.93750000000000,-1.87500000000000,-1.81250000000000,-1.75000000000000,-1.68750000000000,-1.62500000000000,-1.56250000000000,-1.50000000000000,-1.43750000000000,-1.37500000000000,-1.31250000000000,-1.25000000000000,-1.18750000000000,-1.12500000000000,-1.06250000000000,-1,-0.937500000000000,-0.875000000000000,-0.812500000000000,-0.750000000000000,-0.687500000000000,-0.625000000000000,-0.562500000000000,-0.500000000000000,-0.437500000000000,-0.375000000000000,-0.312500000000000,-0.250000000000000,-0.187500000000000,-0.125000000000000,-0.0625000000000000,0,0.0625000000000000,0.125000000000000,0.187500000000000,0.250000000000000,0.312500000000000,0.375000000000000,0.437500000000000,0.500000000000000,0.562500000000000,0.625000000000000,0.687500000000000,0.750000000000000,0.812500000000000,0.875000000000000,0.937500000000000,1,1.06250000000000,1.12500000000000,1.18750000000000,1.25000000000000,1.31250000000000,1.37500000000000,1.43750000000000,1.50000000000000,1.56250000000000,1.62500000000000,1.68750000000000,1.75000000000000,1.81250000000000,1.87500000000000,1.93750000000000,2,2.06250000000000,2.12500000000000,2.18750000000000,2.25000000000000,2.31250000000000,2.37500000000000,2.43750000000000,2.50000000000000,2.56250000000000,2.62500000000000,2.68750000000000,2.75000000000000,2.81250000000000,2.87500000000000,2.93750000000000,3,3.06250000000000,3.12500000000000,3.18750000000000,3.25000000000000,3.31250000000000,3.37500000000000,3.43750000000000,3.50000000000000,3.56250000000000,3.62500000000000,3.68750000000000,3.75000000000000,3.81250000000000,3.87500000000000,3.93750000000000,4,4.06250000000000,4.12500000000000,4.18750000000000,4.25000000000000,4.31250000000000,4.37500000000000,4.43750000000000,4.50000000000000,4.56250000000000,4.62500000000000,4.68750000000000,4.75000000000000,4.81250000000000,4.87500000000000,4.93750000000000,5 };
	float fc = 0.5;
	float fs = 16.0;
	float R = 0.25;
	plist_float n = value_list_float(n_, 161);

	plist_float b = sqrt_design(n, fc, fs, R);
	//plist_float p = value_list_float(a,10);
	//plist_int inx = find(p, 0, 0);
	show_f(b);
	return 0;
}

你可能感兴趣的:(c++,matlab)