Search code examples
cfwrite

How to use 'fwrite' in C?


I'm trying to use 'fwrite' and make snd files. I want to make IIR Filter. I made a FIR Filter and i use the codes to IIR Filter. (of course, change coeffs) But I think 'fwrite' doesn't work. Because the result of IIR filter is only zero. I think I did some mistakes in my code.

Can you give me a hint? i'm totally in panic now.

When I check output[], it looks fine. It has group of integer, as i respect.

I don't know how to write it.

I know the code is so long and looks difficult.

But I have no idea so if you give me a hand, it would be pleasured. Thanks for reading.

#include <stdio.h>
#include <stdint.h>
#include <memory.h>
// Filter Code Definitions
#define MAX_INPUT_LEN 80
#define MAX_FLT_LEN 14
#define BUFFER_LEN (MAX_FLT_LEN - 1 + MAX_INPUT_LEN)

double insamp[BUFFER_LEN];
// IIR inititialization
void IirFloatInit(void)
{
    memset(insamp, 0, sizeof(insamp));
}

// the IIR filter function
void IirFloat(double *coeffs, double *input, double *output, int length, int 
filterLength)
{
    double acc;
    double *coeffp;
    double *inputp;
    int n;
    int k;

// put the new samples at the high end of the buffer
memcpy(&insamp[filterLength - 1], input, length * sizeof(double));
for (n = 0; n < length; n++) {
    coeffp = coeffs;
    inputp = &insamp[filterLength - 1 + n];
    acc = 0;
    for (k = 0; k < filterLength; k++) {
        acc += (*coeffp++) * (*inputp--);
    }
    output[n] = acc;
}
memmove(&insamp[0], &insamp[length], (filterLength - 1) * sizeof(double));
}

double coeffs[MAX_FLT_LEN];

void intToFloat(int16_t *input, double *output, int length)
{
    int i;
    for (i = 0; i < length; i++) {
        output[i] = (double)input[i];
    }
}
void floatToInt(double *input, int16_t *output, int length)
{
    int i;
    for (i = 0; i < length; i++)
    {

        input[i] += 0.5;
        if (input[i] > 32767.0)
        {
            input[i] = 32767.0;
        }
        else if (input[i] < -32768.0)
        {
            input[i] = -32768.0;
        } 
        output[i] = (int16_t)input[i];
    }
}
// number of samples to read per loop
int main(void)
{
    int size;
    int16_t input[MAX_INPUT_LEN];
    int16_t output[MAX_INPUT_LEN];
    double floatInput[MAX_INPUT_LEN];
    double floatOutput[MAX_INPUT_LEN];
    double coeffs[MAX_FLT_LEN];

    FILE *in_fid;
    FILE *out_fid;
    FILE *filter;

    // open the input waveform file
    in_fid = fopen("input.snd", "rb");
    if (in_fid == 0) {
        printf("couldn't open input.snd");
        return;
    }

    // open the output waveform file
    out_fid = fopen("outputFloatIIR.snd", "wb");
    if (out_fid == 0) {
        printf("couldn't open outputFloat.snd");
        return;
    }

    filter = fopen("coeffs_iir.txt", "r");
    if (filter == NULL) {
        puts("couldn't open coeffs_iir.txt");
        return;
    }    
    for (int i = 0; i < MAX_FLT_LEN; i++) {
        fscanf(filter, "%le", &coeffs[i]);
        printf("%le \n", coeffs[i]);
    }


    IirFloatInit();

    do {

        size = fread(input, sizeof(int16_t), MAX_INPUT_LEN, in_fid);

        intToFloat(input, floatInput, size);

        IirFloat(coeffs, floatInput, floatOutput, size, MAX_FLT_LEN);

        floatToInt(floatOutput, output, size);

        fwrite(output, sizeof(int16_t), size, out_fid);
    } while (size != 0);

    fclose(in_fid);
    fclose(out_fid);
    fclose(filter);
    getchar();
    return 0;
}

Solution

  • Into the manual (man printf) I can read this:

    eE The double argument is rounded and converted in the style [-]d.ddde+-dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E' (rather thane') to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00.

    Into your file where coefficients are stored, are into that format? If you use a debugger, what happen with the value read into fscanf?

    I mean, probably the format is not the expected and because of that you get 0. Maybe you want to use fscanf(filter, "%lf", &coeffs[i]); ?

    What is the value returned by fscanf? Into the manual (man fscanf) can be read this:

    RETURN VALUES These functions return the number of input items assigned. This can be fewer than provided for, or even zero, in the event of a matching failure. Zero indicates that, although there was input available, no conversions were assigned; typically this is due to an invalid input character, such as an alphabetic character for a `%d' conversion. The value EOF is returned if an input failure occurs before any conver- sion such as an end-of-file occurs. If an error or end-of-file occurs after conversion has begun, the number of conversions which were successfully completed is returned.