C++ 实现 matlab 的 zp2tf 函数
文章目录
- 1. matlab 的 zp2tf 函数的作用
- 2. matlab 的 zp2tf 函数的使用方法
- 3. C++实现
- 3.1 complex.h 文件
- 3.2 zp2tf.h 文件
- 4. 测试结果
- 4.1 测试文件
- 4.2 测试结果
1. matlab 的 zp2tf 函数的作用
作用是将极点形式的 H(s) 函数的分母展开
2. matlab 的 zp2tf 函数的使用方法
[z, p, k]=buttap(4);
disp("零点:"+z);
disp("极点:"+p);
disp("增益:"+k);[Bap,Aap]=zp2tf(z,p,k);% 由零极点和增益确定归一化Han(s)系数
disp("Bap="+Bap);
disp("Aap="+Aap);
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 zp2tf.h 文件
#pragma once
#include <iostream>
#include <math.h>
#include <vector>
#include "complex.h"#define pi ((double)3.141592653589793)using namespace std;pair<Complex*, int> pair_mul(pair<Complex*, int> p1, pair<Complex*, int> p2)
{pair<Complex*, int> result;Complex* new_coeff = (Complex*)malloc(sizeof(Complex));int ret = complex_mul(p1.first, p2.first, new_coeff);if (ret == -1){cout << "pair_mul error!" << endl;return result;}int new_pow = p1.second + p2.second;result.first = new_coeff;result.second = new_pow;return result;
}vector<pair<Complex*, int>> element_mul(vector<pair<Complex*, int>> element1, vector<pair<Complex*, int>> element2)
{ vector<pair<Complex*, int>> result;if (element1.size() <= 0 || element2.size() <= 0){cout << "element_mul error!" << endl;return result;}for (int i = 0; i < element1.size(); i++){pair<Complex*, int> p1 = element1[i];pair<Complex*, int> p;for (int j = 0; j < element2.size(); j++){pair<Complex*, int> p2 = element2[j];p = pair_mul(p1, p2);if (result.size() == 0){result.push_back(p);}else{bool merge_flg = false;for (int k = 0; k < result.size(); k++){// 如果指数一样,就合并if (result[k].second == p.second){result[k].first->real += p.first->real;result[k].first->img += p.first->img;free(p.first);p.first = NULL;p.second = 0;merge_flg = true;break;}}if (!merge_flg){result.push_back(p);}}}}return result;
}vector<pair<Complex*, int>> zp2tf(vector<Complex*> poles)
{vector<pair<Complex*, int>> tf; // pair 的 first代表极点形式H(s)的分母展开后的每一项的系数,second 代表每一项的指数if (poles.size() <= 0){return tf;}// 先构造 n 个 (s-极点)vector<vector<pair<Complex*, int>>> elements(poles.size());for (int i = 0; i < poles.size(); i++){vector<pair<Complex*, int>> element;pair<Complex*, int> e1;Complex* c1 = (Complex*)malloc(sizeof(Complex));c1->real = -1.0 * poles[i]->real;c1->img = -1.0 * poles[i]->img;e1 = make_pair(c1, 0);// -1.0 * 极点element.push_back(e1);pair<Complex*, int> e2;Complex* c2 = (Complex*)malloc(sizeof(Complex));c2->real = 1.0;c2->img = 0.0;e2 = make_pair(c2, 1);// selement.push_back(e2);elements[i] = element;}if (elements.size() == 1){return elements[0];}// 再将 n 个 (s-极点) 乘起来vector<pair<Complex*, int>> element = elements[0];for (int i = 1; i < poles.size(); i++){vector<pair<Complex*, int>> result = element_mul(element, elements[i]);if (result.size() <= 0){return tf;}element = result;}return element;
}
4. 测试结果
4.1 测试文件
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include "buttap.h"
#include "zp2tf.h"
using namespace std;#define pi ((double)3.141592653589793)int main()
{vector<Complex*> poles = buttap(4);for (int i = 0; i < poles.size(); i++){printf("%.15lf, %.15lf\n", poles[i]->real, poles[i]->img);}vector<pair<Complex*, int>> tf = zp2tf(poles);return 0;
}
4.2 测试结果
3阶模拟低通巴特沃斯滤波器
8阶模拟低通巴特沃斯滤波器
结果与 matlab 均一致,大家可以自行验证
C++ 实现 matlab 的 zp2tf 函数相关推荐
- MATLAB信号处理工具箱函数列表分类
**现将MATLAB信号处理工具箱函数进行分组,便于记忆查询和长期回顾.(只解释基本用途,具体用法请在help目录下查询)** Waveform Generation(波形产生) chairp: 产生 ...
- matlab信号处理工具箱函数列表
现将MATLAB信号处理工具箱函数进行分组,便于记忆查询和长期回顾.(只解释基本用途,具体用法请在help目录下查询) Waveform Generation(波形产生) chairp: 产生扫频余弦 ...
- matlab pup,matlab利用bar函数画不同颜色直方图
matlab利用bar函数画直方图,参考文献[1]是matlab官方提供的help文档.里面提供了bar函数的基本用法,但是没有说明如何在同一张图中,为每个bar设置不同的颜色. 例子代码: myda ...
- Matlab中bwmorph函数的使用
Matlab中bwmorph函数的使用 Matlab中提供了一个基于形态学的处理函数,即以膨胀.腐蚀等操作为基础,其语法格式如下: bw2=bwmorph(bw1,operation,n); 其中bw ...
- 9.matlab中repmat函数
来源: matlab中repmat函数的用法 - CSDN博客 https://blog.csdn.net/anqier1009/article/details/5214978 B = repmat( ...
- 如何在Matlab中获取函数参数的数目?
本图文详细介绍了Matlab中获取函数参数数目的方法.
- c++引用matlab类,matlab调用C++函数浅谈(一)
由于在下才疏学浅,在网上看各高手指南时亦觉云里雾里,遂决定一切说明从最基础说起,一是方便自己(记性奇差),二是方便似我的小白.以下部分是我从各网站论坛等摘抄.重组.改写过的,以求更加详实明朗,由于参考 ...
- matlab语言unique,Matlab的unique函数的C++实现
Matlab中的unique函数,实现的是去除重复元素,只保留一个,且剩下的非重元素按大小排列: C++中stl::unique函数与其不同之处在于:是去除相邻的重复元素,且不改变向量大小,把重复元素 ...
- matlab s% d%,matlab中var函数的翻译For N-D arrays, VAR operates along the first
matlab中var函数的翻译For N-D arrays, VAR operates along the first matlab中var函数的翻译 For N-D arrays, VAR oper ...
- 【 MATLAB 】rem 函数介绍
rem函数和mod函数很相似,二者认真看一个,另一个看一下区别即可. mod函数介绍:[ MATLAB ]mod 函数介绍 rem Remainder after division Syntax r ...
最新文章
- 在if里赋值要注意=和==的优先级,==优先于=
- Servlet--HttpServletRequest一些不常用的方法
- Ajax 的乱码问题(2)
- tbase同步mysql_mysql主从同步
- [zz]写在KVM (Kernel-based Virtual Machine) 安装成功后
- Python开发【Part 7】:常用模块
- SAP License:SAP电话面试
- Linux 命令(112)—— unalias 命令(builtin)
- Spring Boot 热部署(转)
- c语言程序设计徐立辉答案,C语言习题
- UT(XCAP) 参数说明
- Android与iPhone的对比
- android 仿小米商城,仿小米商城网页版(全套)
- 状态机实现的LED交通灯2
- 楼市调控不断升级,房产中介还能翻身吗?
- oracle分区表和分区索引的概念
- 17.3.13 多任务学习 Multi-task learning
- 巧设BIOS,让老主板也支持U盘启动!
- 微信多开工具 Mac版的安装及卸载教程
- excel matlab日期,Excel日期格式在matlab中的转换