C++ 实现 Matlab 的 lp2lp 函数

1. matlab 的 lp2lp 函数的作用

去归一化 H(s) 的分母

2. matlab 的 lp2lp 函数的使用方法

[z, p, k]=buttap(3);
disp("零点:"+z);
disp("极点:"+p);
disp("增益:"+k);

[Bap,Aap]=zp2tf(z,p,k);% 由零极点和增益确定归一化Han(s)系数
disp("Bap="+Bap);
disp("Aap="+Aap);

[Bbs,Abs]=lp2lp(Bap,Aap,86.178823974858318);% 低通到低通 计算去归一化Ha(s),最后一个参数就是去归一化的 截止频率
disp("Bbs="+Bbs);
disp("Abs="+Abs);

3. C++ 实现

3.1 complex.h 文件

#pragma once
#include <iostream>

typedef struct Complex
{
    
    
	double real;// 实数
	double img;// 虚数

	Complex()
	{
    
    
		real = 0.0;
		img = 0.0;
	}

	Complex(double r, double i)
	{
    
    
		real = r;
		img = i;
	}
}Complex;

/*复数乘法*/
int complex_mul(Complex* input_1, Complex* input_2, Complex* output)
{
    
    
	if (input_1 == NULL || input_2 == NULL || output == NULL)
	{
    
    
		std::cout << "complex_mul error!" << std::endl;
		return -1;
	}

	output->real = input_1->real * input_2->real - input_1->img * input_2->img;
	output->img = input_1->real * input_2->img + input_1->img * input_2->real;
	return 0;
}

3.2 lp2lp.h 文件

实现方法很简单,将 H(s) 的分母的系数乘以 pow(wc, 这一项的指数) 即可

#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include "complex.h"

using namespace std;

vector<pair<Complex*, int>> lp2lp(vector<pair<Complex*, int>> tf, double wc)
{
    
    
	vector<pair<Complex*, int>> result;
	if (tf.size() <= 0 || wc <= 0.001)
	{
    
    
		return result;
	}

	result.resize(tf.size());
	for (int i = 0; i < tf.size(); i++)
	{
    
    
		double coeff = pow(wc, tf[i].second);
		Complex* c = (Complex*)malloc(sizeof(Complex));
		c->real = coeff * tf[i].first->real;
		c->img = coeff * tf[i].first->img;
		pair<Complex*, int> p(c, tf[i].second);
		result[i] = p;
	}

	return result;
}

4. 测试结果

4.1 测试文件

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include "buttap.h"
#include "zp2tf.h"
#include "lp2lp.h"
using namespace std;

#define pi ((double)3.141592653589793)

int main()
{
    
    
	vector<Complex*> poles = buttap(3);
	vector<pair<Complex*, int>> tf = zp2tf(poles);
	// 去归一化后的 H(s) 的分母
	vector<pair<Complex*, int>> ap = lp2lp(tf, 86.178823974858318);

	return 0;
}

4.2 测试3阶的情况

在这里插入图片描述

4.3 测试9阶的情况

在这里插入图片描述
可以看出二者结果一样,大家可以自行验证

猜你喜欢

转载自blog.csdn.net/Redmoon955331/article/details/130199447