3 Replies - 333 Views - Last Post: 04 August 2014 - 10:48 AM Rate Topic: -----

#1 rousseau34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 14

AUDIO FILTER PROGRAM HELP><

Posted 04 August 2014 - 08:04 AM

Hello all, I signed here today because I'm done. Already spent like 20 hours trying to create a code
where I could simply read a binary audio file, make some changes in it, and writting it in another file.

The format I'm using here is .pcm.

My main code:

FILE *fr;
FILE *fw;
short int *x;
short int *y;
int n;

x = (short int *)malloc(2*sizeof(short int));
if (x == NULL){
printf("ERRO AO ALOCAR MEMORIA\n");
return 1;
}
y = (short int *)malloc(2*sizeof(short int));
if (y == NULL){
printf("ERRO AO ALOCAR MEMORIA\n");
return 1;
}


y[0] = 0;
y[1] = 0;


fr = fopen("freq_1_4000.pcm", "rb");
if (fr == NULL) exit(1);
fw = fopen("MEDIA_MOVEL_4.pcm", "wb");
if (fw == NULL) exit(1);

while (fread(&x,sizeof(short int),1,fr)==1)
{


for (n=1;n<=2;n++)
{

y[n] = 0.5 * (x[n] + x[n-1]);

}


fwrite (&y, sizeof(short int), 1, fw);
y[0] = y[1];



The problem happens on y[n] = 0.5 * (x[n] + x[n-1]);.

It says
Unhandled exception at 0x00b5157c in MEDIA_MOVEL_4.exe: 0xC0000005: Access violation reading location 0x001aff05.

I searched on the web and I found out that this kind of error usually occurs when the values overload the size of a vector, but I don't see that happening in my code. Help please, I started C a week ago.

Hello all, I signed here today because I'm done. Already spent like 20 hours trying to create a code
where I could simply read a binary audio file, make some changes in it, and writting it in another file.

The format I'm using here is .pcm.

My main code:

FILE *fr;
FILE *fw;
short int *x;
short int *y;
int n;

x = (short int *)malloc(2*sizeof(short int));
if (x == NULL){
printf("ERRO AO ALOCAR MEMORIA\n");
return 1;
}
y = (short int *)malloc(2*sizeof(short int));
if (y == NULL){
printf("ERRO AO ALOCAR MEMORIA\n");
return 1;
}


y[0] = 0;
y[1] = 0;


fr = fopen("freq_1_4000.pcm", "rb");
if (fr == NULL) exit(1);
fw = fopen("MEDIA_MOVEL_4.pcm", "wb");
if (fw == NULL) exit(1);

while (fread(&x,sizeof(short int),1,fr)==1)
{


for (n=1;n<=2;n++)
{

y[n] = 0.5 * (x[n] + x[n-1]);

}


fwrite (&y, sizeof(short int), 1, fw);
y[0] = y[1];



The problem happens on y[n] = 0.5 * (x[n] + x[n-1]);.

It says
Unhandled exception at 0x00b5157c in MEDIA_MOVEL_4.exe: 0xC0000005: Access violation reading location 0x001aff05.

I searched on the web and I found out that this kind of error usually occurs when the values overload the size of a vector, but I don't see that happening in my code. Help please, I started C a week ago.

Is This A Good Question/Topic? 0
  • +

Replies To: AUDIO FILTER PROGRAM HELP><

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: AUDIO FILTER PROGRAM HELP><

Posted 04 August 2014 - 10:22 AM

Quote

The problem happens on y[n] = 0.5 * (x[n] + x[n-1]);.


What is the value of n at the time of the crash? If it is anything other than 1 you will be in undefined territory because anything greater than 1 is out of bounds of your array and when you subtract 1 from n it must never be less than zero.


Jim

This post has been edited by jimblumberg: 04 August 2014 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 rousseau34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 14

Re: AUDIO FILTER PROGRAM HELP><

Posted 04 August 2014 - 10:25 AM

Thanks Jim!

But, if that's the case, how could I use the last x value? I mean the value x had before?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: AUDIO FILTER PROGRAM HELP><

Posted 04 August 2014 - 10:48 AM

Quote

But, if that's the case, how could I use the last x value? I mean the value x had before?

The value before what? Remember arrays in C start at zero and end at size - 1. So with your array of size 2 the valid index values are 0 and 1, anything else would be out of the bounds of your array. Also since you're using a const in your malloc() call there really isn't a need for the malloc() call at all, just statically allocate the array.

And when using for(,,) loops with arrays it is usually a mistake to be using the operator<= you probably should be using the operator< instead. Let's look at the following snippet:

for (n=1;n<=2;n++)
{
   y[n] = 0.5 * (x[n] + x[n-1]);
}



This loop will execute two times, the starting value of n will be 1 and the ending value will be 2 because of the operator<=, remember valid values are zero and one.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1