Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define PI (3.14159265358979323846)
- #define Q 32768
- long s1, s2, amp;
- long a, b, a_base;
- long sa, sa1, sa2, sb, sb1, sb2;
- float r;
- void genIIR(float freq) {
- a = round(2 * r * cos(2*PI * freq)*Q);
- b = round(-r * r * Q);
- sa2 = 0;
- sa1 = round(sin(2 * PI * freq)*Q);
- }
- void genIIRBase(float freq) {
- a_base = round(2 * r * cos(2*PI * freq)*Q);
- b = round(-r * r * Q);
- sb2 = 0;
- sb1 = round(sin(2 * PI * freq)*Q);
- }
- void getBase() {
- sb = (a_base * sb1 >> 15) + (b * sb2 >> 15);
- sb2 = sb1;
- sb1 = sb;
- }
- void getSample() {
- sa = (a * sa1 >> 15) + (b * sa2 >> 15);
- sa2 = sa1;
- sa1 = sa;
- }
- int main() {
- FILE *fptrIn, *fptrOut;
- amp = 32000;
- r = 1.0f;
- float f_sampl = 48000.0;
- const float f_selected = 1336.0/f_sampl;
- const float f_tab[] = {
- 697.0/f_sampl,
- 770.0/f_sampl,
- 852.0/f_sampl,
- 941.0/f_sampl
- };
- fptrIn = fopen("dataIn.txt", "r");
- if (fptrIn == NULL) {
- printf("Error! Problem with dataIn file!");
- exit(1);
- }
- fptrOut = fopen("dataOut.txt", "w");
- if (fptrOut == NULL) {
- printf("Error! Problem with dataOut file!");
- exit(1);
- }
- {
- genIIRBase(f_selected);
- int j = 0;
- for (int i = 0; i < 48000; i++) {
- if (i % 12000 == 0) {
- genIIR(f_tab[j]);
- j++;
- }
- getBase();
- getSample();
- s1 = amp * sb;
- s2 = amp * sa;
- fprintf(fptrOut, "%d\n", (int) (s1 + s2));
- }
- }
- fclose(fptrIn);
- fclose(fptrOut);
- printf("Hello world!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement