0

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.

13
  • 1
    When you print the values, what are the first few outputs? Commented Feb 16, 2022 at 17:06
  • 2
    not getting a linking error for failing to link in the math library? Commented Feb 16, 2022 at 17:06
  • 1
    Hi, I tried on linux (no windows around) and it seems to work like a charm. At least I have a 16000 bytes long cosine.dat file full of data. (compiled with gcc a.c -lm and ran ./a.out) Commented Feb 16, 2022 at 17:14
  • 1
    I get a hex dump of 007D 767B E176 606F 2065 6358 7949 BF38 16-bit little-endian, compiled with MSVC. It repeats every 80 samples. Commented Feb 16, 2022 at 17:27
  • 2
    humor me one more time .. should've said delete the .dat file too... you're sure it's getting overwritten/modified each time you run? I'm still at a loss as to why you're not getting linking errors, maybe mingw on Windows works some behind-the-scenes magic to link in the math library for you? That's not the case in any linux dev environment I've worked in, including the godbolt link from my previous comment (remove -lm and you'll see the linking error(s)). Commented Feb 16, 2022 at 17:47

1 Answer 1

0

!!!!!!!!!!!!! I just fixed the problem!!!!@!!@!@!@!@!@!@

I think THE ANSWER is >>>>> reboot <<<<<

in order to try, I installed Visual Studio and it seemed I needed to reboot. At that point I just thought "oh come to think of it I did not try two basic solutions: 1) reboot computer 2) hit computer". After rebooting, I just complied the file and it was so successful!!! But I'm not sure whether rebooting is the key or installing visual studio is the key. Anyways I think rebooting did some job...

Now everything is clear... C doesn't hate me... it just turns out that something was alienating me from C... Now I'm crying with delight...

Sign up to request clarification or add additional context in comments.

1 Comment

hah, glad you got it working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.