Go Down

### Topic: Question about magnitude (Read 3628 times)previous topic - next topic

#### Noisecontrol

#75
##### Jun 17, 2018, 09:03 pm
Hello peiter
I do that formula but didnt convert magnitude to voltage

#76

#### Noisecontrol

#77
##### Jun 19, 2018, 06:01 pm
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 libraryvoid 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  amplitude0   21   02   03   04   25   06   07   08   19   010   011   012   213   014   015   016   017   018   019   020   50021   022   023   024   125   026   027   028   029   030   031   0Done`

#### PieterP

#78
##### Jun 19, 2018, 06:09 pm
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 pmLast Edit: Jun 19, 2018, 06:20 pm by Noisecontrol
Please write equiption of it for calculate frequency

Where sin wave is not corrected?

#### PieterP

#80
##### Jun 19, 2018, 06:20 pm
It's in the MATLAB script already.

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

#### Noisecontrol

#81
##### Jun 19, 2018, 06:27 pm
Thanks a lot it is ok

#### Noisecontrol

#82
##### Jun 19, 2018, 07:49 pm
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 libraryvoid 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 pmLast 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 libraryvoid 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

#84
##### Jun 19, 2018, 10:11 pm
Filter For what range frequency to remve?
What is UL in calculate frequncy that you wrote?

#### PieterP

#85
##### Jun 19, 2018, 10:19 pm
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 pmLast Edit: Jun 20, 2018, 08:11 pm by Noisecontrol
Hello
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.0075 Hz: AMP:0.60 C:0.00 AMP(A):0.00150 Hz: AMP:0.20 C:0.04 AMP(A):0.01225 Hz: AMP:0.12 C:0.11 AMP(A):0.01300 Hz: AMP:0.10 C:0.20 AMP(A):0.02375 Hz: AMP:0.12 C:0.30 AMP(A):0.04450 Hz: AMP:0.12 C:0.40 AMP(A):0.05525 Hz: AMP:0.00 C:0.51 AMP(A):0.00600 Hz: AMP:0.09 C:0.61 AMP(A):0.05676 Hz: AMP:0.11 C:0.70 AMP(A):0.08751 Hz: AMP:0.06 C:0.78 AMP(A):0.05826 Hz: AMP:0.11 C:0.86 AMP(A):0.09901 Hz: AMP:0.00 C:0.92 AMP(A):0.00976 Hz: AMP:0.36 C:0.98 AMP(A):0.361051 Hz: AMP:0.41 C:1.03 AMP(A):0.421126 Hz: AMP:0.35 C:1.08 AMP(A):0.381201 Hz: AMP:0.11 C:1.12 AMP(A):0.131277 Hz: AMP:0.13 C:1.15 AMP(A):0.151352 Hz: AMP:0.09 C:1.18 AMP(A):0.101427 Hz: AMP:0.15 C:1.21 AMP(A):0.181502 Hz: AMP:0.14 C:1.23 AMP(A):0.171577 Hz: AMP:0.11 C:1.25 AMP(A):0.141652 Hz: AMP:0.14 C:1.27 AMP(A):0.171727 Hz: AMP:0.05 C:1.28 AMP(A):0.071802 Hz: AMP:0.10 C:1.29 AMP(A):0.131878 Hz: AMP:0.09 C:1.31 AMP(A):0.111953 Hz: AMP:0.08 C:1.31 AMP(A):0.102028 Hz: AMP:0.10 C:1.32 AMP(A):0.132103 Hz: AMP:0.03 C:1.33 AMP(A):0.032178 Hz: AMP:0.08 C:1.33 AMP(A):0.112253 Hz: AMP:0.12 C:1.34 AMP(A):0.162328 Hz: AMP:0.00 C:1.34 AMP(A):0.002403 Hz: AMP:0.14 C:1.34 AMP(A):0.192478 Hz: AMP:0.03 C:1.34 AMP(A):0.032554 Hz: AMP:0.06 C:1.34 AMP(A):0.082629 Hz: AMP:0.06 C:1.34 AMP(A):0.082704 Hz: AMP:0.00 C:1.34 AMP(A):0.002779 Hz: AMP:0.06 C:1.34 AMP(A):0.082854 Hz: AMP:0.00 C:1.33 AMP(A):0.002929 Hz: AMP:0.11 C:1.33 AMP(A):0.153004 Hz: AMP:0.05 C:1.33 AMP(A):0.073079 Hz: AMP:0.15 C:1.32 AMP(A):0.203155 Hz: AMP:0.12 C:1.32 AMP(A):0.163230 Hz: AMP:0.10 C:1.31 AMP(A):0.133305 Hz: AMP:0.00 C:1.31 AMP(A):0.003380 Hz: AMP:0.09 C:1.30 AMP(A):0.113455 Hz: AMP:0.06 C:1.30 AMP(A):0.083530 Hz: AMP:0.11 C:1.29 AMP(A):0.153605 Hz: AMP:0.05 C:1.29 AMP(A):0.073680 Hz: AMP:0.03 C:1.28 AMP(A):0.033756 Hz: AMP:0.10 C:1.27 AMP(A):0.123831 Hz: AMP:0.00 C:1.27 AMP(A):0.003906 Hz: AMP:0.10 C:1.26 AMP(A):0.133981 Hz: AMP:0.06 C:1.25 AMP(A):0.084056 Hz: AMP:0.10 C:1.24 AMP(A):0.134131 Hz: AMP:0.08 C:1.24 AMP(A):0.104206 Hz: AMP:0.09 C:1.23 AMP(A):0.114281 Hz: AMP:0.06 C:1.22 AMP(A):0.074357 Hz: AMP:0.08 C:1.21 AMP(A):0.104432 Hz: AMP:0.10 C:1.20 AMP(A):0.124507 Hz: AMP:0.08 C:1.19 AMP(A):0.094582 Hz: AMP:0.11 C:1.19 AMP(A):0.134657 Hz: AMP:0.00 C:1.18 AMP(A):0.004732 Hz: AMP:0.06 C:1.17 AMP(A):0.074807 Hz: AMP:0.00 C:1.16 AMP(A):0.004882 Hz: AMP:0.06 C:1.15 AMP(A):0.074957 Hz: AMP:0.00 C:1.14 AMP(A):0.005033 Hz: AMP:0.00 C:1.13 AMP(A):0.005108 Hz: AMP:0.05 C:1.12 AMP(A):0.065183 Hz: AMP:0.10 C:1.11 AMP(A):0.115258 Hz: AMP:0.14 C:1.10 AMP(A):0.165333 Hz: AMP:0.00 C:1.10 AMP(A):0.005408 Hz: AMP:0.03 C:1.09 AMP(A):0.035483 Hz: AMP:0.10 C:1.08 AMP(A):0.105558 Hz: AMP:0.08 C:1.07 AMP(A):0.085634 Hz: AMP:0.11 C:1.06 AMP(A):0.115709 Hz: AMP:0.08 C:1.05 AMP(A):0.085784 Hz: AMP:0.14 C:1.04 AMP(A):0.145859 Hz: AMP:0.06 C:1.03 AMP(A):0.065934 Hz: AMP:0.09 C:1.02 AMP(A):0.096009 Hz: AMP:0.00 C:1.01 AMP(A):0.006084 Hz: AMP:0.10 C:1.00 AMP(A):0.106159 Hz: AMP:0.00 C:0.99 AMP(A):0.006235 Hz: AMP:0.03 C:0.98 AMP(A):0.036310 Hz: AMP:0.06 C:0.97 AMP(A):0.066385 Hz: AMP:0.00 C:0.96 AMP(A):0.006460 Hz: AMP:0.03 C:0.95 AMP(A):0.026535 Hz: AMP:0.06 C:0.94 AMP(A):0.066610 Hz: AMP:0.05 C:0.94 AMP(A):0.056685 Hz: AMP:0.05 C:0.93 AMP(A):0.056760 Hz: AMP:0.14 C:0.92 AMP(A):0.126836 Hz: AMP:0.10 C:0.91 AMP(A):0.096911 Hz: AMP:0.12 C:0.90 AMP(A):0.116986 Hz: AMP:0.06 C:0.89 AMP(A):0.057061 Hz: AMP:0.08 C:0.88 AMP(A):0.077136 Hz: AMP:0.03 C:0.87 AMP(A):0.027211 Hz: AMP:0.10 C:0.86 AMP(A):0.097286 Hz: AMP:0.06 C:0.85 AMP(A):0.057361 Hz: AMP:0.09 C:0.84 AMP(A):0.077436 Hz: AMP:0.00 C:0.83 AMP(A):0.007512 Hz: AMP:0.03 C:0.83 AMP(A):0.027587 Hz: AMP:0.00 C:0.82 AMP(A):0.007662 Hz: AMP:0.05 C:0.81 AMP(A):0.047737 Hz: AMP:0.03 C:0.80 AMP(A):0.027812 Hz: AMP:0.11 C:0.79 AMP(A):0.087887 Hz: AMP:0.05 C:0.78 AMP(A):0.047962 Hz: AMP:0.08 C:0.77 AMP(A):0.068037 Hz: AMP:0.05 C:0.76 AMP(A):0.048113 Hz: AMP:0.00 C:0.76 AMP(A):0.008188 Hz: AMP:0.08 C:0.75 AMP(A):0.068263 Hz: AMP:0.03 C:0.74 AMP(A):0.028338 Hz: AMP:0.00 C:0.73 AMP(A):0.008413 Hz: AMP:0.00 C:0.72 AMP(A):0.008488 Hz: AMP:0.05 C:0.71 AMP(A):0.048563 Hz: AMP:0.00 C:0.71 AMP(A):0.008638 Hz: AMP:0.00 C:0.70 AMP(A):0.008714 Hz: AMP:0.06 C:0.69 AMP(A):0.048789 Hz: AMP:0.06 C:0.68 AMP(A):0.048864 Hz: AMP:0.06 C:0.67 AMP(A):0.048939 Hz: AMP:0.10 C:0.67 AMP(A):0.079014 Hz: AMP:0.09 C:0.66 AMP(A):0.069089 Hz: AMP:0.11 C:0.65 AMP(A):0.079164 Hz: AMP:0.10 C:0.64 AMP(A):0.069239 Hz: AMP:0.06 C:0.64 AMP(A):0.049315 Hz: AMP:0.06 C:0.63 AMP(A):0.049390 Hz: AMP:0.06 C:0.62 AMP(A):0.049465 Hz: AMP:0.00 C:0.61 AMP(A):0.009540 Hz: AMP:0.06 C:0.61 AMP(A):0.04`

#87

#### Noisecontrol

#88
##### Jun 20, 2018, 08:52 pm
I want to know in th source code ,amplitude that caculated is in range 0 to 1023 or Not

#### PieterP

#89
##### Jun 20, 2018, 09:11 pm
No. It's in the documentation.

Go Up