I'm currently reading a book 'Introducing Speech and Language Processing'.
In the book lets me know a code that generates 200Hz cosine wave.
Below is the code. (file name is coswave.c)
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main() {
int length, status, i;
short int *x;
float freq;
double arg, twopi;
FILE *file_id;
length = 8000;
freq = 0.025;
twopi = 8.0 * atan(1.0);
arg = twopi * freq;
x = (short int *) calloc(length, sizeof(short int));
if (!x) {
printf("Unable to allocate space for samples\n");
exit(1);
}
for (i = 0; i < length; i++)
x[i] = (short int) 32000 * cos(i * arg);
file_id = fopen("cosine.dat", "wb");
if (file_id == NULL) {
printf("Unable to open file\n");
exit(1);
}
status = fwrite(x, sizeof(short int), length, file_id);
if (status < length) {
printf("Unable to write all samples\n");
exit(1);
}
fclose(file_id);
return 0;
}
After writing that code, in the cmd, I typed the below.
gcc -c coswave.c // this command gives me coswave.o file
gcc -o bleep.exe coswave.o // this command gives md bleep.exe file
After that, I executed bleep.exe file and cosine.dat is created.
I opened it with 'hexdump for VSC' extension, but except a first line all contents are filled with 00.
00000000: 00 7D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .}..............
What is wrong here?
for (i = 0; i < length; i++) {
x[i] = (short int) 32000 * cos(i * arg);
printf("%d %d \n", i, x[i]);
}
According to printf(), There is no problem with cosine values.
My OS is Windows and I used mingw gcc. Thank you very much.
007D 767B E176 606F 2065 6358 7949 BF3816-bit little-endian, compiled with MSVC. It repeats every 80 samples.-lmand you'll see the linking error(s)).