"C-Language" PCM audio data processing---Lower sampling rate __c language

Source: Internet
Author: User

PCM data recorded with a microphone, 16bit, 48KHz, mono, and I would like to get the 16KHz sampling rate of the PCM data, then by reducing the sampling rate of the method to achieve 48000HZ to 16000HZ sample rate conversion.

The conversion principle is relatively simple, 48000HZ to 16000HZ, actually dropped 3 times times, in the same time unit interval, 48000HZ sampled 3 points, 16000HZ sampled a point, that is, from the 48000HZ file for each read 3 data, It will be based on these 3 data to calculate the 1 data, and this data corresponds to a 16000HZ file in a data.

The realization of the following C language is implemented concretely

#define Old_file_path "FILE.PCM"
#define Down_file_path "DOWNSAMPLE.PCM"

void Pcm_downsample (void)
{ Short
    sread = 0;
    Short tempsum = 0;
    int size = 0;
    int flag = 0;

    FILE *FP = fopen (Old_file_path, "rb+");
    FILE *fp_down = fopen (Down_file_path, "wb+");

    while (!feof (FP))
    {        
        size = Fread (&sread, 2, 1, FP);//Read two bytes at a time, 16bit
        if (size>0)
        {
            TempSum = TempSum + sread; Sum
            flag++;
            if (flag = = 3)              //If three points are taken
            {
                flag = 0;
                TempSum = TEMPSUM/3;   Average, you can modify according to their needs, not necessarily mean, you can directly take a point
                fwrite (&tempsum, 2, 1, fp_down);    
                TempSum = 0;
            }
        }

    Fclose (FP);
    Fclose (Fp_down);
}

Here I will convert 3 points to 1 points of the algorithm, is the average algorithm, you can use other algorithms to convert also can, if you want to reduce to other sample rate, as long as the corresponding proportion to sample, you can. reference materials

1.PCM data format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.