---
title: SampleDown
description: "信号的降采样，即使用采样因子（factor）降低采样率。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0090.html
sourcePath: /source/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0090.html
indexId: f0bfa3782c88c8881c58c187027a565a8de17a82ab6156eb57ebb7c5f8b5151577
---
# SampleDown

信号的降采样，即使用采样因子（factor）降低采样率。

具体而言，降采样是将源采样序列src按序分为若干个块，每个块包含factor个采样点，丢弃其中的factor-1个采样点，并保存一个采样点至dst。参数phase是源采样序列的相位，它决定了每个块内要保留的采样点的位置。phase的取值范围应为[0, factor-1]。采样结果序列的长度保存在dstLen指向的位置。

处理方式可使用如下公式描述：

；

；

。

函数接口声明如下：

- 整型数的操作：
  HmppResult HMPPS_SampleDown_16s(const int16_t *src, int32_t srcLen, int16_t *dst, int32_t *dstLen, int32_t factor,int32_t *phase);

  HmppResult HMPPS_SampleDown_16sc(const Hmpp16sc *src, int32_t srcLen, Hmpp16sc *dst, int32_t *dstLen, int32_t factor,int32_t *phase);

- 浮点数的操作：
  HmppResult HMPPS_SampleDown_32f(const float *src, int32_t srcLen, float *dst, int32_t *dstLen, int32_t factor, int32_t *phase);

  HmppResult HMPPS_SampleDown_64f(const double *src, int32_t srcLen, double *dst, int32_t *dstLen, int32_t factor, int32_t *phase);

  HmppResult HMPPS_SampleDown_32fc(const Hmpp32fc *src, int32_t srcLen, Hmpp32fc *dst, int32_t *dstLen, int32_t factor, int32_t *phase);

  HmppResult HMPPS_SampleDown_64fc(const Hmpp64fc *src, int32_t srcLen, Hmpp64fc *dst, int32_t *dstLen, int32_t factor, int32_t *phase);


#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| src | 指向源向量的指针。 | 非空 | 输入 |
| srcLen | 源向量长度。 | (0, INT\_MAX] | 输入 |
| dst | 指向目的向量的指针。 | 非空 | 输出 |
| dstLen | 指向目标向量长度的指针。 | 非空 | 输出 |
| factor | 采样因子。 | (0, INT\_MAX] | 输入 |
| phase | 指向采样相位的指针。 | 非空且[0, factor) | 输入 |


#### 返回值

- 成功：返回HMPP_STS_NO_ERR。
- 失败：返回错误码。


#### 错误码

| 错误码 | 描述 |
| --- | --- |
| HMPP\_STS\_NULL\_PTR\_ERR | src、dst、dstLen、phase这几个入参中存在空指针。 |
| HMPP\_STS\_SIZE\_ERR | srcLen小于或等于0。 |
| HMPP\_STS\_SAMPLE\_FACTOR\_ERR | 采样因子小于或等于0。 |
| HMPP\_STS\_SAMPLE\_PHASE\_ERR | 采样相位小于0或者大于或等于factor。 |


#### 示例

```
#define BUFFER_SIZE_S 9
void SampleDownExample(void)
{
Hmpp16sc src1[BUFFER_SIZE_S] = { 14761, -14761, -9981, 9381, 286, -7115, -15360, -7959, -26648, -13094,
-29344, -999, -12922, 8793, -21146, 12262, 1568, -6382 };
Hmpp16sc src2[BUFFER_SIZE_S] = { 30000, -30000, -9976, 9976, -848, -2080, -22268, -32406, 29451, 8620,
19416, -30118, -31166, -28113, -11331, -8179, -30595, 14322 };
Hmpp16sc dst[65] = { 0 };
int32_t dstLen1 = 0;
int32_t dstLen2 = 0;
int32_t factor = 4;
int32_t phase = 2;
HmppResult result;
result = HMPPS_SampleDown_16sc(src1, BUFFER_SIZE_S, dst, &dstLen1, factor, &phase);
result |= HMPPS_SampleDown_16sc(src2, BUFFER_SIZE_S, dst + dstLen1, &dstLen2, factor, &phase);
printf("result = %d\n", result);
if (result != HMPP_STS_NO_ERR) {
return;
}
int32_t i = 0;
printf("dstLen1 = %d\ndst1 =", dstLen1);
for(; i < dstLen1; ++i){
printf(" %d %d   ", dst[i].re, dst[i].im);
}
printf("\ndstLen2 = %d\ndst2 =", dstLen1);
for(; i < dstLen1 + dstLen2; ++i){
printf(" %d %d   ", dst[i].re, dst[i].im);
}
printf("\n");
}
```

运行结果：

```
result = 0
dstLen1 = 2
dst1 = 286 -7115    -12922 8793
dstLen2 = 2
dst2 = -9976 9976    19416 -30118
```
