Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
관리 메뉴

OnlyOne

Practice[MAF] 본문

Control Engineering/C Programming

Practice[MAF]

Taesan Kim 2024. 9. 4. 23:27

Practice[MAF]

 

Intro

In this practice, I will get data from Lida in the Txt file and filter the moving average filter.

 

Header File

/*====================================================================================
Handong Global University
--------------------------------------------------------------------------------------
Name: 		Taesan Kim
ID:			22300203
Create:		2024.07.17
Modifire:	2024.07.17
--------------------------------------------------------------------------------------
레이더 자료를 가져와서 MAF를 적용한 뒤, 새로운 파일에 저장한다.
====================================================================================*/

#ifndef MYLIDAR_H
#define MYLIDAR_H

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


/**
* @breif 레이더 파일 열기
* 레이더 파일을 열어서 정보를 가공한다.
* @param num1: 복소수1
* @param num2: 복소수2
*
* @return: 복소수1과 복소수2를 더한 값
*/

typedef struct _LIDAR
{
	double* range;
	double* angle;
	int		size;
} LIDAR;

typedef struct _STRUCTURE
{
	double* x;
	double* y;
	int size;

} STRUCTURE;

void read_file(const char* file_name, LIDAR* data);

void save_file(const char* file_name, STRUCTURE* data);

void clear_buffer(LIDAR* data);

void clear_structure_buffer(STRUCTURE* data);


/**
* @breif MAF연산기능
* MAF연산을 돕는다.
* @param buffer: MAF의 버퍼값 
* @param array[]: MAF를 적용할 리스트 이름
* @param size: array리스트의 요소 개수
*
* @return: 없음, array리스트에 MAF를 적용한 데이터를 넣어준다.
*/

void maf(int buffer, LIDAR* data);


#endif //MYLIDAR_H

 

 

Source File

#include "../include/myLIDAR.h"

void read_file(const char* file_name, LIDAR* data) {
    FILE* file;
    errno_t err = fopen_s(&file, file_name, "r");

    if (err != 0) {
        perror("Error opening file");
        return;
    }

    if (file == NULL) {
        perror("Error opening file");
        return;
    }

    int word;
    int line_count = 0;

    do {
        word = fgetc(file);
        if (word == '\n') line_count++;
    } while (word != EOF);

    if (line_count < 1) {
        fclose(file);
        return;
    }

    data->range = (double*)malloc(sizeof(double) * line_count);
    data->angle = (double*)malloc(sizeof(double) * line_count);
    data->size = line_count;

    fseek(file, 0, SEEK_SET);

    for (int line = 0; line < line_count; line++) {
        fscanf_s(file, "%lf\t%lf\n", data->range + line, data->angle + line);
    }

    fclose(file);
}

void save_file(const char* file_name, STRUCTURE* data) {
    FILE* file;
    errno_t err = fopen_s(&file, file_name, "w+t");

    if (err != 0) {
        perror("Error opening file");
        return;
    }

    if (file == NULL) {
        perror("Error opening file");
        return;
    }

    for (int i = 0; i < data->size; i++) {
        fprintf_s(file, "%f\t%f\n", data->x[i], data->y[i]);
    }

    fclose(file);
}

void clear_buffer(LIDAR* data)
{
    if (data->range != NULL) {
        free(data->range);
        data->range = NULL;
    }
    if (data->angle != NULL) {
        free(data->angle);
        data->angle = NULL;
    }
}

void clear_structure_buffer(STRUCTURE* data) {
    if (data->x != NULL) {
        free(data->x);
        data->x = NULL;
    }
    if (data->y != NULL) {
        free(data->y);
        data->y = NULL;
    }
}

void maf(int buffer, LIDAR* data) 
{
    data->size;
    int data_size = (*data).size;
    if (buffer > data_size) {
        printf("Buffer cannot bigger than List size!!");
        return;
    }

    for (int i = 0; i < data_size; i++) {
        float sum = 0.0;
        int count = 0;

        for (int j = i; j >= 0 && count < buffer; j--) {
            sum += data->range[j];
            count++;
        }

        data->range[i] = sum / count;
    }
}

 

 

Main

#include "include/myLIDAR.h"



int main() {
    const char* filepath = "lidar_rawdata.txt";
    LIDAR data;
    STRUCTURE data_result;
    STRUCTURE maf_data_result;

    read_file(filepath, &data);

    for (int i = 0; i < data.size; i++) {
        printf("Range: %lf, Angle: %lf\n", data.range[i], data.angle[i]);
    }


    data_result.size = data.size;
    data_result.x = (double*)malloc(data.size * sizeof(double));
    data_result.y = (double*)malloc(data.size * sizeof(double));

    for (int j = 0; j < data.size; j++) {
        data_result.x[j] = data.range[j] * cos(data.angle[j]);
        data_result.y[j] = data.range[j] * sin(data.angle[j]);
    }

    save_file("output.txt", &data_result);
    clear_structure_buffer(&data_result);

    // 버퍼 크기 입력
    int buffer = 5;

    maf(buffer, &data);

    maf_data_result.size = data.size;
    maf_data_result.x = (double*)malloc(data.size * sizeof(double));
    maf_data_result.y = (double*)malloc(data.size * sizeof(double));

    for (int j = 0; j < data.size; j++) {
        maf_data_result.x[j] = data.range[j] * cos(data.angle[j]);
        maf_data_result.y[j] = data.range[j] * sin(data.angle[j]);
    }

    save_file("maf_output.txt", &maf_data_result);

    clear_buffer(&data);
    clear_structure_buffer(&maf_data_result);

    return 0;
}

* You should set the txt file in the same folder as the main.cpp file.

 

lidar_rawdata.txt
0.04MB

'Control Engineering > C Programming' 카테고리의 다른 글

Function&Array  (0) 2024.09.04
배열(Array) 기초  (0) 2024.09.04
Practice[Integral]  (0) 2024.09.04
Practice[Root Formula]  (0) 2024.09.04
구조체(Derived Type)  (0) 2024.09.04