Go Down

Topic: Question about magnitude (Read 1 time) previous topic - next topic

Noisecontrol

Hello peiter
I do that formula but didnt convert magnitude to voltage

PieterP

There's nothing I can do without more information. Post your exact code.

Noisecontrol

Hello
I use this code for anlaize a sin wave with 300 Hz
Why the bin is not correct cuz when i want to calculate freqency  by this equiption fs*bin/N
Frequency is not correct
19200*20/64=6000 Hz but my freqency sin wave was 300Hz!!!


Code: [Select]


#define LIN_OUT 1
 #define FFT_N 64
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

#include <FFT.h> // include the library


void setup() {
  Serial.begin(115200); // output on the serial port
  //19200khz
  sbi(ADCSRA, ADPS2);
  sbi(ADCSRA, ADPS1);
  cbi(ADCSRA, ADPS0);

}

void loop() { 
  int i,k;
  float f1=300; 
   //the two input frequencies (bin values)
  for (i = 0 ; i < FFT_N ; i++)
  {
    k=1000*sin(2*PI*f1*i/FFT_N);
    fft_input[2*i] = k; // put real data into even bins
    //Serial.println(k);
    fft_input[2*i+1] = 0; // set odd bins to 0
  }
  fft_reorder(); // reorder the data before doing the fft
  fft_run(); // process the data using the fft
  fft_mag_lin(); // calculate the magnitude of the output
  // print the frequency index and amplitudes
  Serial.println("bin  amplitude");
  for (i=0; i<FFT_N/2 ; i++) {
    {
      Serial.print(i); Serial.print("   ");
      Serial.println(fft_lin_out[i]);
    }
   
  }
  Serial.println("Done");
  while(1); //wait here
}




And output

Code: [Select]


bin  amplitude
0   2
1   0
2   0
3   0
4   2
5   0
6   0
7   0
8   1
9   0
10   0
11   0
12   2
13   0
14   0
15   0
16   0
17   0
18   0
19   0
20   500
21   0
22   0
23   0
24   1
25   0
26   0
27   0
28   0
29   0
30   0
31   0
Done


PieterP

The result is correct, your original sine wave is not. You have to divide by the sampling rate, not by the number of samples.

Noisecontrol

#79
Jun 19, 2018, 06:17 pm Last Edit: Jun 19, 2018, 06:20 pm by Noisecontrol
Please write equiption of it for calculate frequency


Where sin wave is not corrected?

PieterP

It's in the MATLAB script already.

In Arduino code:
Code: [Select]
int k = 1000 * sin(2*PI*f1*i/19200.0);

Noisecontrol


Noisecontrol

Hello again
I change code to recive signal by a microphone sensor. It show amplitude and frequncy but i dont know why when i make a tone for example 1000 hz arduino show max amplitude in 525 hz or when send a tone with 2000 hz it show max amplitude in 975 Hz or with tone 8000 ,it show 3975
Do i must freqency multiply 2?

Code: [Select]


#define LOG_OUT 1 // use the log output function
#define FHT_N 256 // set to 256 point fht
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

#include <FHT.h> // include the library

void setup() {
  Serial.begin(115200); // use the serial port
  TIMSK0 = 0; // turn off timer0 for lower jitter
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x40; // use adc0
  DIDR0 = 0x01; // turn off the digital input for adc0
    //19200khz
  sbi(ADCSRA, ADPS2);
  sbi(ADCSRA, ADPS1);
  cbi(ADCSRA, ADPS0);
}

void loop() {
 // { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
      while(!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fht_input[i] = k; // put real data into bins
    }
    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    fht_mag_log(); // take the output of the fht
    sei();
    Serial.println("start");
    for (byte i = 0 ; i < FHT_N/2 ; i++) {
   Serial.print(i*19200/FHT_N); Serial.print("/");
      Serial.println(fht_log_out[i]); // send out the data
    }
 // } while(1);
}


PieterP

#83
Jun 19, 2018, 09:30 pm Last Edit: Jun 19, 2018, 09:45 pm by PieterP
Your ADC configuration is all wrong. You're sampling at the wrong rate, because you reset the ADC configuration after the first measurement. Try this:
Code: [Select]
#define LOG_OUT 1 // use the log output function
#define FHT_N 256 // set to 256 point fht
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

#include <FHT.h> // include the library

void setup() {
  Serial.begin(115200); // use the serial port
  ADCSRB = 0; // Free running mode
  ADMUX = (DEFAULT << 6) | 0; // A0
  sbi(ADCSRA, ADEN); // Enable ADC
  sbi(ADCSRA, ADATE); // Auto trigger
  sbi(ADCSRA, ADSC); // Start conversion
  // 19.231 kHz
  sbi(ADCSRA, ADPS2);
  sbi(ADCSRA, ADPS1);
  cbi(ADCSRA, ADPS0);
}

void loop() {
  do {
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
      while (!(ADCSRA & _BV(ADIF))); // wait for adc to be ready
      sbi(ADCSRA, ADIF); // Clear interrupt flag
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fht_input[i] = k; // put real data into bins
    }
    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    fht_mag_log(); // take the output of the fht
    sei();
    for (uint8_t i = 0 ; i < FHT_N / 2 ; i++) {
      Serial.print(i * 19231UL / FHT_N); Serial.print(" Hz:\t");
      Serial.println(fht_log_out[i]); // send out the data
    }
  } while(0);
  while(1);
}


By the way, you need a strong anti-aliasing filter, especially at these low sampling rates.

Noisecontrol

Filter For what range frequency to remve?
What is UL in calculate frequncy that you wrote?

PieterP

Everything above half the sampling rate must be attenuated as much as possible. Read up on the Nyquist-Shannon theorem.

UL means unsigned long. An int would overflow.

Noisecontrol

#86
Jun 20, 2018, 08:10 pm Last Edit: Jun 20, 2018, 08:11 pm by Noisecontrol
Hello
Peiter thanks for your help
Your code was correct
Now i could calculated correction factor for in per bin and convert dB to dBA

Code: [Select]


0 Hz: AMP:0.65 C:0.00 AMP(A):0.00
75 Hz: AMP:0.60 C:0.00 AMP(A):0.00
150 Hz: AMP:0.20 C:0.04 AMP(A):0.01
225 Hz: AMP:0.12 C:0.11 AMP(A):0.01
300 Hz: AMP:0.10 C:0.20 AMP(A):0.02
375 Hz: AMP:0.12 C:0.30 AMP(A):0.04
450 Hz: AMP:0.12 C:0.40 AMP(A):0.05
525 Hz: AMP:0.00 C:0.51 AMP(A):0.00
600 Hz: AMP:0.09 C:0.61 AMP(A):0.05
676 Hz: AMP:0.11 C:0.70 AMP(A):0.08
751 Hz: AMP:0.06 C:0.78 AMP(A):0.05
826 Hz: AMP:0.11 C:0.86 AMP(A):0.09
901 Hz: AMP:0.00 C:0.92 AMP(A):0.00
976 Hz: AMP:0.36 C:0.98 AMP(A):0.36
1051 Hz: AMP:0.41 C:1.03 AMP(A):0.42
1126 Hz: AMP:0.35 C:1.08 AMP(A):0.38
1201 Hz: AMP:0.11 C:1.12 AMP(A):0.13
1277 Hz: AMP:0.13 C:1.15 AMP(A):0.15
1352 Hz: AMP:0.09 C:1.18 AMP(A):0.10
1427 Hz: AMP:0.15 C:1.21 AMP(A):0.18
1502 Hz: AMP:0.14 C:1.23 AMP(A):0.17
1577 Hz: AMP:0.11 C:1.25 AMP(A):0.14
1652 Hz: AMP:0.14 C:1.27 AMP(A):0.17
1727 Hz: AMP:0.05 C:1.28 AMP(A):0.07
1802 Hz: AMP:0.10 C:1.29 AMP(A):0.13
1878 Hz: AMP:0.09 C:1.31 AMP(A):0.11
1953 Hz: AMP:0.08 C:1.31 AMP(A):0.10
2028 Hz: AMP:0.10 C:1.32 AMP(A):0.13
2103 Hz: AMP:0.03 C:1.33 AMP(A):0.03
2178 Hz: AMP:0.08 C:1.33 AMP(A):0.11
2253 Hz: AMP:0.12 C:1.34 AMP(A):0.16
2328 Hz: AMP:0.00 C:1.34 AMP(A):0.00
2403 Hz: AMP:0.14 C:1.34 AMP(A):0.19
2478 Hz: AMP:0.03 C:1.34 AMP(A):0.03
2554 Hz: AMP:0.06 C:1.34 AMP(A):0.08
2629 Hz: AMP:0.06 C:1.34 AMP(A):0.08
2704 Hz: AMP:0.00 C:1.34 AMP(A):0.00
2779 Hz: AMP:0.06 C:1.34 AMP(A):0.08
2854 Hz: AMP:0.00 C:1.33 AMP(A):0.00
2929 Hz: AMP:0.11 C:1.33 AMP(A):0.15
3004 Hz: AMP:0.05 C:1.33 AMP(A):0.07
3079 Hz: AMP:0.15 C:1.32 AMP(A):0.20
3155 Hz: AMP:0.12 C:1.32 AMP(A):0.16
3230 Hz: AMP:0.10 C:1.31 AMP(A):0.13
3305 Hz: AMP:0.00 C:1.31 AMP(A):0.00
3380 Hz: AMP:0.09 C:1.30 AMP(A):0.11
3455 Hz: AMP:0.06 C:1.30 AMP(A):0.08
3530 Hz: AMP:0.11 C:1.29 AMP(A):0.15
3605 Hz: AMP:0.05 C:1.29 AMP(A):0.07
3680 Hz: AMP:0.03 C:1.28 AMP(A):0.03
3756 Hz: AMP:0.10 C:1.27 AMP(A):0.12
3831 Hz: AMP:0.00 C:1.27 AMP(A):0.00
3906 Hz: AMP:0.10 C:1.26 AMP(A):0.13
3981 Hz: AMP:0.06 C:1.25 AMP(A):0.08
4056 Hz: AMP:0.10 C:1.24 AMP(A):0.13
4131 Hz: AMP:0.08 C:1.24 AMP(A):0.10
4206 Hz: AMP:0.09 C:1.23 AMP(A):0.11
4281 Hz: AMP:0.06 C:1.22 AMP(A):0.07
4357 Hz: AMP:0.08 C:1.21 AMP(A):0.10
4432 Hz: AMP:0.10 C:1.20 AMP(A):0.12
4507 Hz: AMP:0.08 C:1.19 AMP(A):0.09
4582 Hz: AMP:0.11 C:1.19 AMP(A):0.13
4657 Hz: AMP:0.00 C:1.18 AMP(A):0.00
4732 Hz: AMP:0.06 C:1.17 AMP(A):0.07
4807 Hz: AMP:0.00 C:1.16 AMP(A):0.00
4882 Hz: AMP:0.06 C:1.15 AMP(A):0.07
4957 Hz: AMP:0.00 C:1.14 AMP(A):0.00
5033 Hz: AMP:0.00 C:1.13 AMP(A):0.00
5108 Hz: AMP:0.05 C:1.12 AMP(A):0.06
5183 Hz: AMP:0.10 C:1.11 AMP(A):0.11
5258 Hz: AMP:0.14 C:1.10 AMP(A):0.16
5333 Hz: AMP:0.00 C:1.10 AMP(A):0.00
5408 Hz: AMP:0.03 C:1.09 AMP(A):0.03
5483 Hz: AMP:0.10 C:1.08 AMP(A):0.10
5558 Hz: AMP:0.08 C:1.07 AMP(A):0.08
5634 Hz: AMP:0.11 C:1.06 AMP(A):0.11
5709 Hz: AMP:0.08 C:1.05 AMP(A):0.08
5784 Hz: AMP:0.14 C:1.04 AMP(A):0.14
5859 Hz: AMP:0.06 C:1.03 AMP(A):0.06
5934 Hz: AMP:0.09 C:1.02 AMP(A):0.09
6009 Hz: AMP:0.00 C:1.01 AMP(A):0.00
6084 Hz: AMP:0.10 C:1.00 AMP(A):0.10
6159 Hz: AMP:0.00 C:0.99 AMP(A):0.00
6235 Hz: AMP:0.03 C:0.98 AMP(A):0.03
6310 Hz: AMP:0.06 C:0.97 AMP(A):0.06
6385 Hz: AMP:0.00 C:0.96 AMP(A):0.00
6460 Hz: AMP:0.03 C:0.95 AMP(A):0.02
6535 Hz: AMP:0.06 C:0.94 AMP(A):0.06
6610 Hz: AMP:0.05 C:0.94 AMP(A):0.05
6685 Hz: AMP:0.05 C:0.93 AMP(A):0.05
6760 Hz: AMP:0.14 C:0.92 AMP(A):0.12
6836 Hz: AMP:0.10 C:0.91 AMP(A):0.09
6911 Hz: AMP:0.12 C:0.90 AMP(A):0.11
6986 Hz: AMP:0.06 C:0.89 AMP(A):0.05
7061 Hz: AMP:0.08 C:0.88 AMP(A):0.07
7136 Hz: AMP:0.03 C:0.87 AMP(A):0.02
7211 Hz: AMP:0.10 C:0.86 AMP(A):0.09
7286 Hz: AMP:0.06 C:0.85 AMP(A):0.05
7361 Hz: AMP:0.09 C:0.84 AMP(A):0.07
7436 Hz: AMP:0.00 C:0.83 AMP(A):0.00
7512 Hz: AMP:0.03 C:0.83 AMP(A):0.02
7587 Hz: AMP:0.00 C:0.82 AMP(A):0.00
7662 Hz: AMP:0.05 C:0.81 AMP(A):0.04
7737 Hz: AMP:0.03 C:0.80 AMP(A):0.02
7812 Hz: AMP:0.11 C:0.79 AMP(A):0.08
7887 Hz: AMP:0.05 C:0.78 AMP(A):0.04
7962 Hz: AMP:0.08 C:0.77 AMP(A):0.06
8037 Hz: AMP:0.05 C:0.76 AMP(A):0.04
8113 Hz: AMP:0.00 C:0.76 AMP(A):0.00
8188 Hz: AMP:0.08 C:0.75 AMP(A):0.06
8263 Hz: AMP:0.03 C:0.74 AMP(A):0.02
8338 Hz: AMP:0.00 C:0.73 AMP(A):0.00
8413 Hz: AMP:0.00 C:0.72 AMP(A):0.00
8488 Hz: AMP:0.05 C:0.71 AMP(A):0.04
8563 Hz: AMP:0.00 C:0.71 AMP(A):0.00
8638 Hz: AMP:0.00 C:0.70 AMP(A):0.00
8714 Hz: AMP:0.06 C:0.69 AMP(A):0.04
8789 Hz: AMP:0.06 C:0.68 AMP(A):0.04
8864 Hz: AMP:0.06 C:0.67 AMP(A):0.04
8939 Hz: AMP:0.10 C:0.67 AMP(A):0.07
9014 Hz: AMP:0.09 C:0.66 AMP(A):0.06
9089 Hz: AMP:0.11 C:0.65 AMP(A):0.07
9164 Hz: AMP:0.10 C:0.64 AMP(A):0.06
9239 Hz: AMP:0.06 C:0.64 AMP(A):0.04
9315 Hz: AMP:0.06 C:0.63 AMP(A):0.04
9390 Hz: AMP:0.06 C:0.62 AMP(A):0.04
9465 Hz: AMP:0.00 C:0.61 AMP(A):0.00
9540 Hz: AMP:0.06 C:0.61 AMP(A):0.04



 :(

PieterP

#87
Jun 20, 2018, 08:31 pm Last Edit: Jun 20, 2018, 09:09 pm by PieterP
What is your question?

Noisecontrol

I want to know in th source code ,amplitude that caculated is in range 0 to 1023 or Not

PieterP


Go Up