Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

Converting array to 8-bit codes in c

I have an array of 200 elements. I want to divide this array into 25 parts and find the RMS values ??of these parts and print 0 if it is less than 20, 1 if it is greater.

Example

signal1[200]

Output

data[8]={0,1,0,0,1,0,1,1)

I find code for RMS calculation

#include <stdio.h>
#include <math.h>

double rms(double* v, int n)
{
    int i;
    double sum = 0.0;
    for (i = 0; i < n; i++)
        sum += v[i] * v[i];
    return sqrt(sum / n);
}

int main(void)
{
    double v[] = {3,-3,7,1,-3,9,8,1,3,2,-6,-4,-1,-6,7,-6,-6,-7,-6,-1,-4,9,-1,-7,9,48,-6,-39,-24,-9,10,-24,10,21,-28,-
39,-21,-18,-8,1,-42,-24,30,-48,43,23,-1,8,-27,-4,47,5,2,-27,-1,13,18,-11,-13,49,-47,39,42,30,-41,-24,-17,18,-
37,22,-40,16,-1,28,22,41,39,-17,20,-31,-47,25,0,-2,41,11,12,36,31,8,-32,-26,39,-48,-1,-34,48,21,0,-3,-9,4,-10,-
9,0,-8,7,7,5,-7,3,0,10,3,6,-1,-1,7,-9,-8,-7,-2,7,6,-9,-10,3,-8,16,13,-21,-7,-49,49,-34,-40,-13,-30,-1,-16,46,42,-
45,24,-23,-8,5,45,-8,49,-20,20,17,4,20,17,-33,-38,50,-33,-47,6,39,17,-31,-13,-4,49,-35,36,15,-12,-31,-7,-2,-
8,2,-6,-2,2,-5,-4,2,-5,7,10,5,-3,2,-8,9,8,7,-5,2,-10,-2,-4,-7,-7};
   
    printf("%f
", rms(v, sizeof(v) / sizeof(double)));
   
   

    return 0;
}

can you help me please ?

question from:https://stackoverflow.com/questions/65857641/converting-array-to-8-bit-codes-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simply call the function rms with 25 elements at a time:

int main(void)
{
    double v[] = {3,-3,7,1,-3,9,8,1,3,2,-6,-4,-1,-6,7,-6,-6,-7,-6,-1,-4,9,-1,-7,9,48,-6,-39,-24,-9,10,-24,10,21,-28,-
39,-21,-18,-8,1,-42,-24,30,-48,43,23,-1,8,-27,-4,47,5,2,-27,-1,13,18,-11,-13,49,-47,39,42,30,-41,-24,-17,18,-
37,22,-40,16,-1,28,22,41,39,-17,20,-31,-47,25,0,-2,41,11,12,36,31,8,-32,-26,39,-48,-1,-34,48,21,0,-3,-9,4,-10,-
9,0,-8,7,7,5,-7,3,0,10,3,6,-1,-1,7,-9,-8,-7,-2,7,6,-9,-10,3,-8,16,13,-21,-7,-49,49,-34,-40,-13,-30,-1,-16,46,42,-
45,24,-23,-8,5,45,-8,49,-20,20,17,4,20,17,-33,-38,50,-33,-47,6,39,17,-31,-13,-4,49,-35,36,15,-12,-31,-7,-2,-
8,2,-6,-2,2,-5,-4,2,-5,7,10,5,-3,2,-8,9,8,7,-5,2,-10,-2,-4,-7,-7};   
    int data[8], i;

    for (i = 0; i < 8; i++) {
        data[i] = rms(v + 25 * i, 25) > 20.0;
    }
    for (i = 0; i < 8; i++) {
        printf(" %d", data[i]);
    }
    putchar('
');       
    return 0;
}

The expression v + i gives you the address of the i:th element of v.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...