Here's a C program that reads a binary file and performs convolution:
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to perform 1D convolution
void convolve1D(float *input, int inputSize, float *kernel, int kernelSize, float *output) {
int outputSize = inputSize - kernelSize + 1;
for (int i = 0; i < outputSize; i++) {
output[i] = 0.0;
for (int j = 0; j < kernelSize; j++) {
output[i] += input[i + j] * kernel[kernelSize - 1 - j];
}
}
}
// Function to read binary file
float* readBinaryFile(const char *filename, int *size) {
FILE *file = fopen(filename, "rb");
if (!file) {
printf("Error: Cannot open file %s\n", filename);
return NULL;
}
// Get file size
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
// Calculate number of floats
*size = fileSize / sizeof(float);
// Allocate memory
float *data = (float*)malloc(fileSize);
if (!data) {
printf("Error: Memory allocation failed\n");
fclose(file);
return NULL;
}
// Read data
size_t bytesRead = fread(data, 1, fileSize, file);
if (bytesRead != fileSize) {
printf("Error: Failed to read complete file\n");
free(data);
fclose(file);
return NULL;
}
fclose(file);
return data;
}
// Function to write binary file
int writeBinaryFile(const char *filename, float *data, int size) {
FILE *file = fopen(filename, "wb");
if (!file) {
printf("Error: Cannot create output file %s\n", filename);
return 0;
}
size_t bytesWritten = fwrite(data, sizeof(float), size, file);
fclose(file);
return bytesWritten == size;
}
// Function to create a sample binary file for testing
void createSampleFile(const char *filename) {
float sampleData[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
writeBinaryFile(filename, sampleData, 10);
printf("Created sample file with 10 values\n");
}
int main(int argc, char *argv[]) {
const char *inputFile = "input.bin";
const char *outputFile = "output.bin";
// Define convolution kernel (example: moving average)
float kernel[] = {0.25, 0.5, 0.25}; // Simple smoothing kernel
int kernelSize = sizeof(kernel) / sizeof(kernel[0]);
// Check command line arguments
if (argc > 1) {
inputFile = argv[1];
}
if (argc > 2) {
outputFile = argv[2];
}
// Create sample file if needed (for testing)
if (argc > 3 && strcmp(argv[3], "--create-sample") == 0) {
createSampleFile(inputFile);
}
// Read input file
int inputSize;
float *inputData = readBinaryFile(inputFile, &inputSize);
if (!inputData) {
return 1;
}
printf("Read %d float values from %s\n", inputSize, inputFile);
// Calculate output size
int outputSize = inputSize - kernelSize + 1;
if (outputSize <= 0) {
printf("Error: Input size too small for the kernel\n");
free(inputData);
return 1;
}
// Allocate output buffer
float *outputData = (float*)malloc(outputSize * sizeof(float));
if (!outputData) {
printf("Error: Memory allocation failed for output\n");
free(inputData);
return 1;
}
// Perform convolution
convolve1D(inputData, inputSize, kernel, kernelSize, outputData);
// Write output file
if (writeBinaryFile(outputFile, outputData, outputSize)) {
printf("Convolution complete. Output written to %s\n", outputFile);
printf("Output size: %d values\n", outputSize);
} else {
printf("Error: Failed to write output file\n");
}
// Display results (first few values)
printf("\nFirst few values:\n");
printf("Input: ");
for (int i = 0; i < (inputSize < 5 ? inputSize : 5); i++) {
printf("%.2f ", inputData[i]);
}
printf("\nOutput: ");
for (int i = 0; i < (outputSize < 5 ? outputSize : 5); i++) {
printf("%.2f ", outputData[i]);
}
printf("\n");
// Clean up
free(inputData);
free(outputData);
return 0;
}
This program includes the following features:
Key Components:
convolve1D() - Performs 1D convolution between input signal and kernel
readBinaryFile() - Reads floating-point numbers from a binary file
writeBinaryFile() - Writes the convolution result to a binary file
createSampleFile() - Creates a sample input file for testing
Usage:
bash
# Compile the program
gcc -o convolve convolve.c -lm
# Create a sample file and run convolution
./convolve input.bin output.bin --create-sample
# Or use existing binary file
./convolve mydata.bin result.bin
Extending the Program:
To adapt this for different use cases, you can:
Different data types: Replace float with double, int, etc.
2D convolution (for images):
c
void convolve2D(float **input, int rows, int cols,
float **kernel, int kRows, int kCols,
float **output) {
// 2D convolution implementation
}
Custom kernels: Modify the kernel array for different filters:
Padding options: Add zero-padding or other boundary handling methods
The program assumes the binary file contains single-precision floating-point numbers. Make sure your input file matches this format or modify the code accordingly.