2

This is the code I wrote in python that extracts data from a .wav file, applies pre-emphasis, divide into frames of 0.025ms with 0.010 stride, and applies a hamming window:

import scipy.io.wavfile as wavfile
import numpy as np

samplerate, data = wavfile.read(filename)
window = np.hamming(int(winlen*samplerate))

# Pre-Emphasis
for i in range(1, len(data)):
    data[i] = data[i] - 0.97 * data[i-1]

# Framing
dlen = int((len(data)/samplerate - winlen)/stride)+1
for i in range(dlen):
    stpt = int(samplerate*stride)*i
    datalen = int(winlen*samplerate)
    framedata = data[stpt:stpt+datalen].copy()
    frames.append(framedata)

# Apply Window
for i in range(len(frames)):
    for j in range(int(winlen*samplerate)):
        frames[i][j] *= window[j]

Pretty standard, nothing wrong here imo.

Here is the code I wrote in C to do the exact same operation.

#include <iostream>
#include <cmath>

double *hammingwindow;
hammingwindow = (double*)calloc(framelen, sizeof(double));
short *data = new short[10000000];
double **data_frames;
double **data_frames = Declare2DArray(parameters.numframes, parameters.framelen);

// declare hamming window
for (int i=0; i<framelen; i++) {
     hammingwindow[i] = double(0.54) - double(0.46) * cos(2 * PI * double(i) / double(framelen - 1));
}

// apply pre-emphasis (both C and python set to 0.97)
for (int i=1; i<parameters.datasize; i++) {
    data[i] = data[i] - parameters.preemphasis * data[i-1];
}

// Divide into frames
int startpoint = parameters.stride * parameters.samplefreq;

for (int i=0; i<parameters.numframes; i++) {
    for (int j=0; j<parameters.framelen; j++) {
        data_frames[i][j] = data[startpoint*i + j];
    }
}

// Multiply hamming window to frames
for (int i=0; i<parameters.numframes; i++) {
    for (int j=0; j<parameters.framelen; j++) {
        data_frames[i][j] *= parameters.hammingwindow[j];
    }
}

(Conditions)

  • sample rate: 16000
  • sample .wav file is 1 second long
  • The results show no difference in the data read and data that is divided into frames (checked separately)
  • Data from C is passed to python through a binary file, which is saved in C and read in python

(Problem) The python window function and the C window function shows a very marginal difference, fluctuating between +4e-7 ~ -4e07.

However, the windowed data shows a rather consistent abs(difference) of 0.99~1.0 at around 180th~220th frames, out of 400 frames. That's around the dead center of the frames. I don't get how this happens, because the magnitude values of the frames differ from the single digits to the hundreds. The magnitude difference fluctuates a lot, but the difference in the central values AFTER the window function has been applied is CONSISTENT. How??

Could someone give me an explanation of how this might happen, or any idea they might have?

FloopyBeep
  • 21
  • 1
  • 2
    That C code isn't a [mre]. Please try to create a [mre] to show us. Oh, and that "C" code is actually **C++**. It's a weird mix of C and C++, please stick to *one* language. – Some programmer dude Apr 21 '23 at 05:54
  • 1
    I also recommend you scale back the code to narrow down the problem, perhaps even start over from an empty `main` function. Then add one small and simple bit of code at a time, build with extra warnings (treated as errors) and test. Do not continue with the next small and simple bit until the current code build cleanly and passes all tests. That will make it much easier to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your code when there is a problem. – Some programmer dude Apr 21 '23 at 05:57
  • And please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [how to write the "perfect" question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/), especially its [checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And learn how to [edit] your questions to improve them. – Some programmer dude Apr 21 '23 at 07:12
  • 1
    to add to comments above, try your code first on some simple example, e.g. sine with increasing magnitude. It will be easier to see if the problem is C or Python (you should create signal so that you know exactly what are expected values) – dankal444 Apr 21 '23 at 10:35
  • Thank you for the feedback. I have since checked the code and found a few differences that was causing the problem. The reason I have a strange mixture of C and C++ code is because I was prototyping an algorithm using C++ that was likely to be ported to C once finished. I was more familiar with C++, so during development I was using a few styles from C++ that stuck. You're right that doing one or the other is the better approach. – FloopyBeep Apr 28 '23 at 02:35
  • I thought the code written here was concise enough for reproducability, but I read it again and you are correct that it should be more clear and straightforward, especially with the reading from files part. I have resolved the problem on my own so I won't edit the question, but your feedback is valid. – FloopyBeep Apr 28 '23 at 02:37

0 Answers0