博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
依据波形的转折点文件,转换成波形文件
阅读量:6112 次
发布时间:2019-06-21

本文共 3620 字,大约阅读时间需要 12 分钟。

注:输入的input.csv文件末尾必须有换行符结束,最后一行的index必须是200,不然程序无法正常执行。

相应的測试文件在博客的最后

#include "stdafx.h"

#include <string.h>
#include <math.h>
#include <stdlib.h>
#define PEAK_VOLT 33.8
#define WAVE_END_FLAG 5
#define WAVE_MIDDLE_FLAG 0 
#define MAX_INDEX 200
typedef unsigned char BYTE;
typedef struct tagPointCoor{
int x_Index;
double y_Volt;
}PointCoor;
typedef struct tagPointInfo{
PointCoor Coor;
double Percent;
int PosFlag;
}PointInfo;
char WriteFilePath[] = "c:\\my_path\\output.csv";
char ReadFilePath[] = "c:\\my_path\\input.csv";
int WritePointInfo(FILE *fp, PointInfo *Point);
int WriteLine(FILE *fp, const PointCoor *StartPoint, const PointCoor *EndPoint);
int GetPosFlag(const PointCoor *Point);
void WriteWave(FILE *Input, FILE *Output);
void GetNextPoint(FILE *Input, PointCoor *Point);
void WritePoint(FILE *fp, PointCoor *Point);
int _tmain(int argc, _TCHAR* argv[])
{
FILE *Output = NULL;
FILE *Input = NULL;
//PointInfo RandomPoint;
//PointCoor StartPoint;
//PointCoor EndPoint;
//PointCoor Point;
//
//StartPoint.x_Index = 1;
//StartPoint.y_Volt = 33;
//EndPoint.x_Index = 100;
//EndPoint.y_Volt = 33;
//RandomPoint.Coor.x_Index = 2;
//RandomPoint.Coor.y_Volt = 20;
//RandomPoint.Percent = 0.4;
//RandomPoint.PosFlag = 5;
Input = fopen(ReadFilePath, "r");
Output = fopen(WriteFilePath, "w");
if((Input == NULL)||(Output == NULL))
{
return -1;
}
WriteWave(Input, Output);
system("pause");
}
void GetNextPoint(FILE *Input, PointCoor *Point)
{
char ch;
char StrBuf[10] = {0};
int Cnt = 0;
int CommaPos = 0;
ch = fgetc(Input);
while(ch != '\n')
{
switch(CommaPos)
{
case 0: //index
{
if(ch == ',')
{
Point->x_Index = atoi(StrBuf);
memset(StrBuf, 0, 10);
CommaPos++;
Cnt = 0;
break;
}
StrBuf[Cnt++] = ch;
}
break;
case 1: //voltage
{
StrBuf[Cnt++] = ch;
}
break;
default:
break;
}
ch = fgetc(Input);
}
Point->y_Volt = atof(StrBuf);
}
void WriteWave(FILE *Input, FILE *Output)
{
PointCoor StartPoint;
PointCoor NextPoint;
int PosFlag;
GetNextPoint(Input, &StartPoint);
do{
GetNextPoint(Input, &NextPoint);
WriteLine(Output, &StartPoint, &NextPoint);
StartPoint = NextPoint;
if((PosFlag = GetPosFlag(&NextPoint)) == WAVE_END_FLAG)
{
WritePoint(Output, &NextPoint);
}
}while(PosFlag != WAVE_END_FLAG);
}
void WritePoint(FILE *fp, PointCoor *Point)
{
PointInfo RandomPoint;
RandomPoint.Coor.x_Index = Point->x_Index;
RandomPoint.Coor.y_Volt = Point->y_Volt;
RandomPoint.Percent = RandomPoint.Coor.y_Volt / PEAK_VOLT;
RandomPoint.PosFlag = GetPosFlag(Point);
WritePointInfo(fp, &RandomPoint);
}
int WritePointInfo(FILE *fp, PointInfo *Point)
{
char WriteBuf[20] = {0};
if(fp == NULL)
return -1;
sprintf(WriteBuf, "%d,%.2lf,%d\n", Point->Coor.x_Index, Point->Percent, Point->PosFlag);
printf("%s", WriteBuf);
printf("the strlen(Writebuf) is %d \n ", strlen(WriteBuf));
fwrite(WriteBuf, strlen(WriteBuf), 1, fp);
return 0;
}
int GetPosFlag(const PointCoor *Point)
{
int PosFlag;
if(Point->x_Index >= MAX_INDEX)
{
PosFlag = WAVE_END_FLAG;
}
else
{
PosFlag = WAVE_MIDDLE_FLAG;
}
return PosFlag;
}
int WriteLine(FILE *fp, const PointCoor *StartPoint, const PointCoor *EndPoint)
{
PointInfo RandomPoint;
double Slope;
if(fp == NULL)
{
return -1;
}
RandomPoint.Coor.x_Index = StartPoint->x_Index;
Slope = (EndPoint->y_Volt - StartPoint->y_Volt)/(EndPoint->x_Index - StartPoint->x_Index);
while(RandomPoint.Coor.x_Index < EndPoint->x_Index)
{
RandomPoint.Coor.y_Volt = Slope*(RandomPoint.Coor.x_Index - StartPoint->x_Index) + StartPoint->y_Volt;
RandomPoint.Percent = RandomPoint.Coor.y_Volt / PEAK_VOLT;
RandomPoint.PosFlag = GetPosFlag(&RandomPoint.Coor);
WritePointInfo(fp, &RandomPoint);
RandomPoint.Coor.x_Index++;
}
return 0;

}

你可能感兴趣的文章
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
【FJOI2015】金币换位问题
查看>>
数学之美系列二十 -- 自然语言处理的教父 马库斯
查看>>
Android实现自定义位置无标题Dialog
查看>>
面试总结
查看>>
Chrome浏览器播放HTML5音频没声音的解决方案
查看>>
easyui datagrid 行编辑功能
查看>>
类,对象与实例变量
查看>>
HDU 2818 (矢量并查集)
查看>>
【转】php字符串加密解密
查看>>
22. linux 常用命令
查看>>
ASP.Net 使用GridView模板删除一行的用法
查看>>
(十六)字段表集合
查看>>
JPGraph
查看>>
实验二 Java面向对象程序设计
查看>>
------__________________________9余数定理-__________ 1163______________
查看>>
webapp返回上一页 处理
查看>>
新安装的WAMP中phpmyadmin的密码问题
查看>>
20172303 2017-2018-2 《程序设计与数据结构》第5周学习总结
查看>>
eclipse中将一个项目作为library导入另一个项目中
查看>>