John Sampson napisal(a):
> m.topczewski@gmail.com wrote:
> > Hello,
> > I'm new to dsp, still much to learn.
> > I'm working on something that will apply window function to signal read
> > from a .wav file.
> > Until now I've developed a code that reads the data and loads it into
> > memory buffer.
> > The code looks like:
> > "int main(int argc, char *argv[])
> > {
> > FILE *fwyj,*fout;
> > double ts,tk,time;
> > float f0,pr,window;
> > int z,N;
> >
> > long start_byte,end_byte;
> > long new_data_size=0;
> >
> > fwyj = fopen("c:\\input.wav","rb");
> > if (fwyj==NULL)
> > {
> > printf("ERROR: File is missing !!\n");
> > system("PAUSE");
> > exit(1);
> > }
> >
> > fout = fopen("output.wav","wb");
> >
> > if (fwyj)
> > {
> > BYTE id[5], *sound_buffer;
> > id[4]='\0';
> > DWORD size;
> > short format_tag, channels, block_align, bits_per_sample;
> > DWORD format_length, sample_rate, avg_bytes_sec, data_size;
> >
> > fread(id, sizeof(BYTE), 4, fwyj);
> > fwrite(id,sizeof(BYTE), 4,fout);
> >
> > if (!strcmp((char *)id, "RIFF"))
> > { fread(&size, sizeof(DWORD), 1, fwyj);
> > fwrite(&size, sizeof(DWORD), 1, fout);
> > fread(id, sizeof(BYTE), 4, fwyj);
> > fwrite(id, sizeof(BYTE), 4, fout);
> > if (!strcmp((char *)id,"WAVE"))
> > {
> > fread(id, sizeof(BYTE), 4, fwyj);
> > fwrite(id, sizeof(BYTE), 4, fout);
> > fread(&format_length, sizeof(DWORD),1,fwyj);
> > fwrite(&format_length, sizeof(DWORD),1,fout);
> > fread(&format_tag, sizeof(short), 1, fwyj);
> > fread(&channels, sizeof(short),1,fwyj);
> > fread(&sample_rate, sizeof(DWORD), 1, fwyj);
> > fread(&avg_bytes_sec, sizeof(DWORD), 1, fwyj);
> > fread(&block_align, sizeof(short), 1, fwyj);
> > fread(&bits_per_sample, sizeof(short), 1, fwyj);
> > fread(id, sizeof(BYTE), 4, fwyj);
> >
> > fwrite(&format_tag, sizeof(short), 1, fout);
> > fwrite(&channels, sizeof(short),1,fout);
> > fwrite(&sample_rate, sizeof(DWORD), 1, fout);
> > fwrite(&avg_bytes_sec, sizeof(DWORD), 1, fout);
> > fwrite(&block_align, sizeof(short), 1, fout);
> > fwrite(&bits_per_sample, sizeof(short), 1, fout);
> > fwrite(id, sizeof(BYTE), 4, fout);
> >
> > fread(&data_size, sizeof(DWORD), 1, fwyj);
> > fwrite(&data_size, sizeof(DWORD), 1, fout);
> >
> > sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size);
> > fread(sound_buffer, sizeof(BYTE), data_size, fwyj);
> > N=4096; //may be different
> > for (z=0;z<N;z++)
> > {
> > //windowing
> > window=0.54-0.46*cos(2*3.14*z/N);//hamming window function
> > sound_buffer[z]=window*sound_buffer[z];
> > printf("window = %f",window);
> > printf("buffer = %d \n",sound_buffer[z]);
> >
> > }
> > //write data after windowing
> > fwrite(sound_buffer,sizeof(BYTE),data_size,fout);
> > }
> >
> > }
> >
> > }
> > system("PAUSE");
> > return EXIT_SUCCESS;
> > }
> >
> > Assuming that the file is small enough it will be possible to process
> > it in the window function.
> > I cant get it to work - i get many distorions of the signal, sound
> > becomes unreadable - can someone help me with, becouse i went through
> > it many times, and still no clue about what is wrong.
> >
> > Any help would appreciated.
> >
>
> It looks like it will only work if the file contains exactly 4096
> samples. There is also the possibility that the wave header isn't being
> read correctly. Have you confirmed that sound_buffer[0] contains the
> very first audio sample in the file, and that data_size is correct?
>
> John
I'm sure, that the data_size is correct, and that the correct sample is
in the buffer.
Doesn't matter what the size of N is, it doesn't work still for numbers
like 1024 or 2048.
I guess that it is not the case of size of window. Assume that input
file is 0.2 sec long.
Problem may be related (but i'm not sure) with the fact that each
sample is divided into two elements of array.