-3

First we are reading from the files and forming arrays, then we are calculating hanning window(hanning fn) for each array sample , finally we are multiplying(mul fn) the array and the hanning window to form the array final.

#include <stdio.h>
#include<math.h>
#include <stdlib.h>
#include </usr/local/src/libsndfile-1.0.25/libsndfile-1.0.25/src/sndfile.h>
#include </usr/local/src/libsndfile-1.0.25/libsndfile- 1.0.25/src/sndfile.h.in>

void hanning(int);
float w[256];
float mul(float*,int*,float*,float*,int);
int main()
{
    SNDFILE *sf1,*sf2,*sf3,*sf4,*sf5,*sout;
    SF_INFO info1,info2,info3,info4,info5,infout;
    int num_channels;
    int  num_items1,num_items2,num_items3,num_items4,num_items5;
    int num1,num2,num3,num4,num5;
    int *buf1,*buf2,*buf3,*buf4,*buf5;
    int f1,f2,f3,f4,f5;
    int sr1,sr2,sr3,sr4,sr5;
    int c1,c2,c3,c4,c5,d;
    int i,j=0,N=128,k=0;
    float t[128],w1[64],w2[64];

 //    FILE *out;
    hanning(N);
 /* Open the WAV file. */
    info1.format = 0;
    info2.format=0;
    info3.format=0;
    info3.format=0;
    info4.format=0;
    info5.format=0;
    sf1 = sf_open("/mnt/usb2/voice/a.wav",SFM_READ,&info1);
    sf2 = sf_open("/mnt/usb2/voice/na1.wav",SFM_READ,&info2);
    sf3 = sf_open("/mnt/usb2/voice/ma.wav",SFM_READ,&info3);
    sf4 = sf_open("/mnt/usb2/voice/ra__.wav",SFM_READ,&info4);
    sf5 = sf_open("/mnt/usb2/voice/ttha.wav",SFM_READ,&info5);

    if (sf1 == NULL)
        {
        printf("Failed to open the file.\n");
        exit(-1);
        }
/* Print some of the info, and figure out how much data to read. */


    c1 = info1.channels;
    c2 = info2.channels;
    c3 = info3.channels;
    c4 = info4.channels;
    c5 = info5.channels;

f1 = info1.frames;
    f2 = info2.frames;
    f3 = info3.frames;
f4 = info4.frames;
    f5 = info5.frames;

    sr1 = info2.samplerate;
    sr2 = info2.samplerate;
    sr3 = info3.samplerate;
    sr4 = info4.samplerate;
    sr5 = info5.samplerate;

 // printf("frames=%d\n",f);
// printf("samplerate=%d\n",sr);
//printf("channels=%d\n",c);
    num_items1 = f1*c1;
    num_items2 = f2*c2;
    num_items3 = f3*c3;
    num_items4 = f4*c4;
    num_items5 = f5*c5;

//printf("num_items=%d\n",num_items);
/* Allocate space for the data to be read, then read it. */
    buf1 = (int *) malloc(num_items1*sizeof(int));
    buf2 = (int *) malloc(num_items2*sizeof(int));
    buf3 = (int *) malloc(num_items3*sizeof(int));
    buf4 = (int *) malloc(num_items4*sizeof(int));
    buf5 = (int *) malloc(num_items5*sizeof(int));

    num1 = sf_read_int(sf1,buf1,num_items1);
    num2 = sf_read_int(sf2,buf2,num_items2);
    num3 = sf_read_int(sf3,buf3,num_items3);
    num4 = sf_read_int(sf4,buf4,num_items4);
    num5 = sf_read_int(sf5,buf5,num_items5);

for(i=0;i<128;i++)
{
    if(i<64){
        w1[j]=t[i];
        j++;}

        else{
        w2[k]=t[i];
        k++;}
}


x1 = (float *) malloc(num_items1*sizeof(float));
x2 = (float *) malloc(num_items2*sizeof(float));
x3 = (float *) malloc(num_items3*sizeof(float));
x4 = (float *) malloc(num_items4*sizeof(float));
x5 = (float *) malloc(num_items5*sizeof(float));
mul(x1,buf1,w1,w2,num_items1);
mul(x2,buf2,w1,w2,num_items2);
mul(x3,buf3,w1,w2,num_items3);
mul(x4,buf4,w1,w2,num_items4);
mul(x5,buf5,w1,w2,num_items5);
 //printf("num=%d\n",num);

sf_close(sf1);
sf_close(sf2);
sf_close(sf3);
sf_close(sf4);
sf_close(sf5);


d=num_items1+num_items2+num_items3+num_items4+num_items5;
final = (float *) malloc(d*sizeof(float));

for(j=0;j<num_items1;j++){
    final[i]=x1[j];
    i++;}
for(j=0;j<num_items1;j++){
    final[i]=x1[j];
    i++;}
for(j=0;j<num_items1;j++){
    final[i]=x1[j];
    i++;}
for(j=0;j<num_items3;j++){
    final[i]=x3[j];
    i++;}
for(j=0;j<num_items4;j++){
    final[i]=x4[j];
    i++;}
for(j=0;j<num_items1;i++){
    final[i]=x1[j];
    i++;}
for(j=0;j<num_items5;j++){
    final[i]=x5[j];
    i++;}
   //sout=sf_open(final,SFM_READ,&infout);


 // printf("Read %d items\n",num);
/* Write the data to filedata.out. */
/*    out = fopen("filedata.txt","w");
if(out==NULL)
    { printf("Error!");
    exit(1); } 
printf("a");
 for (i = 0; i < 100; i++)
     {

        fprintf(out,"%d ",final[i]);
        fprintf(out,"/n");
    }
    fclose(out);*/
    return 0;
 }

void hanning(int N)
{
    int half, i, idx, n,j=0,k=0;

    float PI=3.1428;
  //  w = (float*) calloc(N, sizeof(float));
 //    memset(w, 0, N*sizeof(float));
 n = N;

    if(n%2==0)
    {
        half = n/2;
        for(i=0; i<half; i++)//Calculates Hanning window samples.
            {w[i] = 0.5 * (1 - cos(2*PI*(i+1) / (n+1)));
            printf("%f\n",w[i]);}

    idx = half-1;
    for(i=half; i<n; i++) {
        w[i] = w[idx];
        printf("%f\n",w[i]);
        idx--;
    }
}
    else
    {
        half = (n+1)/2;
        for(i=0; i<half; i++) //Calculates Hanning window for samples 
            w[i] = 0.5 * (1 - cos(2*PI*(i+1) / (n+1)));
            printf("%f\n",w[i]);
  }
}


float mul(float *x,int *buf,float *w1,float *w2,int k)/*multiplication of hanning window and array*/
{
    float final_1[k],final_2[k];
    int i;
    for(i=0;i<k;i++){
        if(i<64)
        final_1[i]=w1[i];
        else
        final_1[i]=1;}

    for(i=0;i<k;i++){
        if(i<k-64)
        final_2[i]=1;
        else
        final_2[i]=w2[i];
    }

    for(i=0;i<k;i++){
    x[i]=final_1[i]*final_2[i]*buf[i];
    printf("%f\n",x[i]);
    } 
}
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
Apurva
  • 1
  • 5
    Where does it SEGV? What's the stack trace when it does? What's the address it SEGVs on? And you have almost zero error checking for failed file opens or `malloc()` calls. – Andrew Henle Apr 14 '15 at 04:02
  • 3
    Think about factorize your code, so many copy/paste – Ôrel Apr 14 '15 at 04:26
  • You allocate memory for `d` floats, but later fill them in seven loops, four of them over `num_items1` and none over `num_items2`. I'm pretty sure that's not what you want. – M Oehm Apr 14 '15 at 04:56
  • Consider using a debugger to narrow it down. That's a lot of code. – Retired Ninja Apr 14 '15 at 05:05

1 Answers1

1

You should initialize your loop variables closer to the actual loops:

There is a missing i=0; after final = (float *) malloc(d*sizeof(float));

This is definitely a bug, but there are other ones: the size you allocate for the final array is probably too small for the copies you make into it: 4 copies of x1, one copy of x3, x4 and x5, but none of x2 (probably a copy/paste bug here).

Also consider using double instead of float for better accuracy in these computations and use M_PI from <math.h> instead of hard coding a very inaccurate value for pi.

Also try and indent your code more consistently, use K&R style for improved readability.

chqrlie
  • 131,814
  • 10
  • 121
  • 189