当前位置: 首页 > news >正文

C++实现窗函数

1、h

#ifndef WIN4C_H__
#define WIN4C_H__
#pragma once#define PI  3.141592653589793#define EPS  1e-8#include <vector>std::vector<double> rectangle(int);
std::vector<double> boxcar(int); //same as matlab, rectangle window.
std::vector<double> bartlett(int);
std::vector<double> hanning(int, bool symmetric=true);
std::vector<double> hamming(int);
std::vector<double> blackman(int);
std::vector<double> kaiser(int, double alpha=0.5);
std::vector<double> gausswin(int, double alpha=2.5);
std::vector<double> chebwin(int, double r = 100);
std::vector<double> triang(int);
std::vector<double> tukeywin(int, double r = 0.5);
double I0(double alpha);/*NOTE: Use the HANN function to get a Hanning window which has thefirst and last zero-weighted samples.
*/
std::vector<double> hann(int, bool symmetric = true);#endif

2、cpp

#include "win4c.h"
#include "fftw3.h"std::vector<double> boxcar(int n)
{return rectangle(n);
}std::vector<double> rectangle(int n)
{std::vector<double> win(n, 1);return win;
}std::vector<double> bartlett(int n)
{std::vector<double> win(n);for (int i = 0; i < (n + 1) / 2; ++i){win[i] = 2.0 * i / (n - 1.0);win[n - 1 - i] = win[i];}return win;
}std::vector<double> hanning(int n, bool symmetric)
{std::vector<double> win(n);if (symmetric){for (int i = 0; i < (n + 1) / 2; ++i){win[i] = 0.5 - 0.5 * cos(2 * PI * (i + 1) / (n + 1));win[n - 1 - i] = win[i];}}else{win.push_back(0);std::vector<double> tmp = hanning(n - 1, true);win.insert(win.begin() + 1, tmp.begin(), tmp.end());}return win;
}std::vector<double> hamming(int n)
{std::vector<double> win(n);for (int i = 0; i < (n + 1) / 2; ++i){win[i] = (0.54 - 0.46 * cos (2 * PI * i / (n - 1.0)));win[n - 1 - i] = win[i];}return win;
}std::vector<double> blackman(int n)
{std::vector<double> win(n);for (int i = 0; i < (n + 1) / 2; ++i){win[i] = (0.42 - 0.50 * cos(2 * PI * i / (n - 1.0)) + 0.08 * cos(2 * 2 * PI * i / (n - 1.0)));win[n - 1 - i] = win[i];}return win;
}std::vector<double> kaiser(int n, double alpha)
{std::vector<double> win(n);for (int i = 0; i < (n + 1) / 2; ++i){double beta = 2 * alpha * sqrt(i*(n - i - 1.0)) / (n - 1.0);win[i] = I0(beta) / I0(alpha);win[n - 1 - i] = win[i];}return win;
}std::vector<double> gausswin(int n, double alpha)
{std::vector<double> win(n);double center = (n - 1) / 2.0;for (int i = 0; i < (n + 1) / 2; ++i){double tmp = alpha * (i - center) / center;win[i] = exp(-0.5 * tmp * tmp);win[n - 1 - i] = win[i];}return win;
}std::vector<double> chebwin(int M, double r)
{std::vector<double> win(M);double order = M - 1.0;double beta = cosh(1.0 / order * acosh(pow(10, (abs(r) / 20.))));double *x = new double[M];for (int i = 0; i < M; i++){double temp = cos(PI * i / M)*beta;if (temp > 1){temp = cosh(order * acosh(temp));}            else if (temp < -1){temp = (2 * (M % 2) - 1) * cosh(order * acosh(-temp));}else{temp = cos(order * acos(temp));}            x[i] = temp;}fftw_complex *fftIn = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * M);fftw_complex *fftOut = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * M);fftw_plan plan = fftw_plan_dft_1d(M, fftIn, fftOut, FFTW_FORWARD, FFTW_MEASURE);double maxvalue(-1e8);if (M % 2)//M ����
    {for (int j = 0; j < M; j++){fftIn[j][0] = x[j];fftIn[j][1] = 0;}fftw_execute(plan);int n = (M + 1) / 2; win[n - 1] = fftOut[0][0];maxvalue = fftOut[0][0];for (int j = n - 1; j >= 1; j--){win[n - 1 - j] = fftOut[j][0];win[j + n - 1] = fftOut[j][0];if (maxvalue < fftOut[j][0]){maxvalue = fftOut[j][0];}                }}else{for (int j = 0; j < M; j++){fftIn[j][0] = x[j] * cos(PI / M * j);fftIn[j][1] = x[j] * sin(PI / M * j);}fftw_execute(plan);int n = M / 2 + 1;for (int j = n - 1; j >= 1; j--){win[n - 1 - j] = fftOut[j][0];win[j + n - 2] = fftOut[j][0];if (maxvalue < fftOut[j][0]) {maxvalue = fftOut[j][0];}                }}for (int j = 0; j < M; j++){win[j] = win[j] / maxvalue;}delete [] x;x = nullptr;fftw_destroy_plan(plan);fftw_free(fftIn);fftw_free(fftOut);return win;
}std::vector<double> triang(int n)
{std::vector<double> win(n);for (int i = 0; i < (n + 1) / 2; ++i){win[i] = (2.0 * ( i + 1) - 1 + n % 2) / (n + n % 2);win[n - 1 - i] = win[i];}return win;
}std::vector<double> hann(int n, bool symmetric)
{std::vector<double> win(n);win.push_back(0);std::vector<double> tmp = hanning(n - 2, symmetric);win.insert(win.begin() + 1, tmp.begin(), tmp.end());win.push_back(0);return win;
}std::vector<double> tukeywin(int n, double r)
{if (r <= 0){return boxcar(n);}if (r >= 1){return hann(n);}std::vector<double> win(n);std::vector<double> t(n);double step = 1.0 / (n - 1);for (int i = 0; i < n - 1; i++){t[i] = i * step;}t[n - 1] = 1;double per = r / 2.0;int tl = floor(per * (n - 1)) + 1;int th = n - tl + 1;//Window is defined in three sections : taper, constant, taper//    w = [((1 + cos(pi / per * (t(1:tl) - per))) / 2);  ones(th - tl - 1, 1); ((1 + cos(pi / per * (t(th:end) - 1 + per))) / 2)];for (int i = 0; i < tl; i++){win[i] = (1 + cos(PI / per * (t[i] - per))) / 2.0;}for (int i = tl; i < th - 1; i++){win[i] = 1;}for (int i = th - 1; i < n; i++){win[i] = (1 + cos(PI / per * (t[i] - 1 + per))) / 2.0;}return win;
}
double I0(double alpha)
{double dNew;double K = alpha / 2.0;const int MAXTERM = 25 + 1;double J = 1.0, dOld = 1.0;bool converge = false;// Use series expansion definition of Bessel.for (int i = 1; i < MAXTERM; ++i){J *= K / i;dNew = dOld + J * J;if ((dNew - dOld) < EPS){converge = true;break;}dOld = dNew;}if (!converge)return 0;return dNew;
}

 

参考:MATLAB窗函数与FIR1函数的C实现:MATLAB窗函数与FIR1函数的C++实现本仓库提供了一系列MATLAB中常用的窗函数以及FIR1函数的C++实现 - AtomGit | GitCode

 

http://www.jsqmd.com/news/436222/

相关文章:

  • 2026订机票哪家平台好?主流平台功能与优惠对比 - 品牌排行榜
  • 2026徐州装修公司哪家好?本地口碑企业推荐 - 品牌排行榜
  • 警惕!XSS攻击的4大真实危害,比你想象中更可怕(附案例)
  • 2026年知名的家具母婴板 工厂推荐:轻奢风母婴板工厂直供哪家专业 - 行业平台推荐
  • 2026年口碑好的定制家具 品牌推荐:四川布艺定制家具稳定供应商推荐 - 行业平台推荐
  • 聚焦健康家居品质升级:2026国内最新适配全屋定制需求的十大环保板材厂家 - 十大品牌榜
  • 智原扩大UMC 14纳米工艺IP布局 锁定边缘AI与消费级市场
  • 【三星小窍门】三星笔记备份综合指南(3种方法)
  • 2026年深圳二手服务器回收推荐:至诚电脑中心,全品类电子产品回收专业服务商 - 品牌推荐官
  • 毕业季福利包:专业高性价比的 AI 论文写作平台,今天全部分享给你!
  • 「权威评测」2026年四川省五大有机肥厂家实力推荐,谁才是靠谱之选? - 深度智识库
  • 2026年防锈漆厂家推荐排行榜:环氧/水性/醇酸/锌黄/带锈/铁红/中灰/钢结构/船舶/镀锌防锈漆,专业防腐与长效防护口碑之选 - 品牌企业推荐师(官方)
  • 如何将视频从 iPhone 传输到Mac
  • 2026年3月科普科技节设备道具租赁品牌推荐,专业配套口碑之选 - 品牌鉴赏师
  • Spring Boot 中配置了全局异常处理器,免try...catch
  • 2026年全国HDPE双壁波纹管厂家推荐榜 靠谱优质实力强 适配小区排水 - 深度智识库
  • 2026年靠谱的絮凝剂聚合硫酸铁 品牌推荐:工业级聚合硫酸铁/水处理剂聚合硫酸铁/污水处理剂聚合硫酸铁工厂直供哪家专业 - 行业平台推荐
  • Agent核心模式?
  • 模块
  • 深度学习音乐流派分类:从零构建摇滚/古典/流行分类器
  • 2026年评价高的数据中心综合布线 品牌推荐:河南数据中心蓄电池/数据中心绿色节能PUE/数据中心柴油发电机优选服务推荐公司 - 行业平台推荐
  • 2026年3月AI互动设备租赁服务推荐,专业方案与品牌保障口碑之选 - 品牌鉴赏师
  • AI-agent :知识问答小助手需求及技术设计
  • 基于GSM的家庭安防系统设计 本设计是基于GSM短信模块的家庭安防报警系统,主要实现以下功能
  • why Capital will fall but East successed. Without observerss effects.
  • 2026年3月机器人巡检服务商推荐,专业方案与品牌保障口碑之选 - 品牌鉴赏师
  • 时序数据库 Apache IoTDB V2.0.7/V1.3.7 发布|安全加固与稳定性优化
  • 2026年靠谱的低压接触器 公司推荐:高低压接触器/高压直流接触器实力品牌厂家推荐 - 行业平台推荐
  • 2026年靠谱的石墨烯电池 厂家推荐:山东石墨烯电池厂家热销推荐 - 行业平台推荐
  • crewCTF 2025 -- Hypervison