---
title: SampleUp
description: "信号的上采样，即使用采样因子（factor）增加采样率。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2200/accel/kunpengaccel_hmpp_06_0091.html
sourcePath: /source/zh/kunpengboostkithistory/2200/accel/kunpengaccel_hmpp_06_0091.html
indexId: baef50fe1357128b9ff5721044e1f177ed774e934b89daaa0082a2a6b325e7e975
---
# SampleUp

信号的上采样，即使用采样因子（factor）增加采样率。

具体而言，上采样是在源采样序列src的每个元素之间插入factor-1个0。因此，在采样结果序列中，factor个元素构成一个块。参数phase是采样相位，它决定了源序列元素在结果序列中的位置，它的取值范围应为[0, factor-1]。

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

；

；

。

函数接口声明如下：

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

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

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

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

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

  HmppResult HMPPS_SampleUp_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 | len小于或等于0。 |
| HMPP\_STS\_SAMPLE\_FACTOR\_ERR | 采样因子小于或等于0。 |
| HMPP\_STS\_SAMPLE\_PHASE\_ERR | 采样相位小于0或者大于或等于factor。 |


#### 示例

```
#define BUFFER_SIZE_S 9
void SampleUpExample(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_SampleUp_16sc(src1, BUFFER_SIZE_S, dst, &dstLen1, factor, &phase);
result |= HMPPS_SampleUp_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 = 36
dst1 = 0 0    0 0    14761 -14761    0 0    0 0    0 0    -9981 9381    0 0    0 0    0 0    286 -7115    0 0    0 0    0 0    -15360 -7959    0 0    0 0    0 0    -26648 -13094    0 0    0 0    0 0    -29344 -999    0 0    0 0    0 0    -12922 8793    0 0    0 0    0 0    -21146 12262    0 0    0 0    0 0    1568 -6382    0 0
dstLen2 = 36
dst2 = 0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    0 0    -31166 -28113    0 0    0 0    0 0    -11331 -8179    0 0    0 0    0 0    -30595 14322    0 0
```
