avatar

松果工作室

欢迎光临

  • 首页
  • freeRTOS
  • ESP
  • 开发手册
  • 快速笔记
  • 个人收藏
  • 工具
Home 峰值查询算法
文章

峰值查询算法

Posted 2024-05-10 Updated 2024-05- 12
By YCP
13~16 min read

这是来源[https://zhuanlan.zhihu.com/p/549588865]
他用了 python, 于是我就用 ChatGPT 转换成了 C 语言, 试验了下, 还算好用
这么牛逼的代码, 我这辈子写不出来了
不求甚解, 会用就行

#include <stdio.h>
#include <stdlib.h>

int* AMPD(float* data, int count, int* peak_count) {
  int* p_data = (int*)calloc(count, sizeof(int));
  int* arr_rowsum = (int*)malloc(sizeof(int) * (count / 2));
  int min_index;
  int max_window_length;

  if (p_data == NULL || arr_rowsum == NULL) {
    fprintf(stderr, "Memory allocation failed\n");
    exit(EXIT_FAILURE);
  }

  // Calculate arr_rowsum
  for (int k = 1; k <= count / 2; k++) {
    int row_sum = 0;
    for (int i = k; i < count - k; i++) {
      if (data[i] > data[i - k] && data[i] > data[i + k]) {
        row_sum -= 1;
      }
    }
    arr_rowsum[k - 1] = row_sum;
  }

  // Find min_index
  min_index = 0;
  for (int i = 1; i < count / 2; i++) {
    if (arr_rowsum[i] < arr_rowsum[min_index]) {
      min_index = i;
    }
  }

  max_window_length = min_index + 1;

  // Mark peaks in p_data
  for (int k = 1; k <= max_window_length; k++) {
    for (int i = k; i < count - k; i++) {
      if (data[i] > data[i - k] && data[i] > data[i + k]) {
        p_data[i] += 1;
      }
    }
  }

  // Find indices of peaks
  int* peaks = (int*)malloc(sizeof(int) * count);
  *peak_count = 0;
  for (int i = 0; i < count; i++) {
    if (p_data[i] == max_window_length) {
      peaks[*peak_count] = i;
      (*peak_count)++;
    }
  }

  // Resize peaks array
  peaks = (int*)realloc(peaks, sizeof(int) * (*peak_count));

  free(p_data);
  free(arr_rowsum);

  return peaks;
}

int main() {
  float data[] = {1.0, 2.0, 3.0, 2.5, 2.0, 1.0, 0.5, 1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0};
  int count = sizeof(data) / sizeof(data[0]);
  int peak_count;

  int* peak_indices = AMPD(data, count, &peak_count);

  printf("Peak indices:\n");
  for (int i = 0; i < peak_count; i++) {
    printf("%d ", peak_indices[i]);
  }
  printf("\n");

  free(peak_indices);

  return 0;
}

坑和笔记
Others
License:  CC BY 4.0
Share

Further Reading

Dec 23, 2024

其他笔记

EC800K AT连接移远云 配置过程 # 配置产品信息(初次连接需配置) AT+QIOTCFG="productinfo","pxxxxt","cDVTxxxxxxxxWGVB" # 连接开发者中心 AT+QIOTREG=1 # 查询当前连接状态(+QIOTSTATE: 8为正常) AT+QI

Jun 21, 2024

环形滤波算法

#include <stdio.h> #include <stdlib.h> #define BUFFER_SIZE 10 // 缓冲区大小 #define THRESHOLD 180

Jun 17, 2024

STM32 ADC采集的三种方式

采样周期 单个采集模式 ADC_Settings: 程序使用 uint16_t ADC_Read(

OLDER

freeRTOS 高精度定时任务

NEWER

F1C200s (一) 硬件原理图

Recently Updated

  • ESP32(八) 简单的webserver
  • ESP32(七) NVS
  • ESP32(四) STA & AP
  • 多级菜单
  • ESP32(五) ESP32 OTA

Trending Tags

WCH Linux Elec freeRTOS STM ESP Flutter Others SwiftUI

Contents

©2025 松果工作室. Some rights reserved.

Using the Halo theme Chirpy