Ura

Dependencies:   CMSIS_DSP_401 mbed

Committer:
Sergeev
Date:
Tue Nov 11 16:41:53 2014 +0000
Revision:
5:173ae8477ae1
Parent:
4:397e8f4699df
Child:
6:0de27f5ec409
q

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergeev 0:b712470aea1d 1 #include "mbed.h"
Sergeev 1:9b1df0b2507d 2 #include <ctype.h>
Sergeev 0:b712470aea1d 3 #include "arm_math.h"
Sergeev 1:9b1df0b2507d 4 #include "arm_const_structs.h"
Sergeev 1:9b1df0b2507d 5
Sergeev 1:9b1df0b2507d 6 void arm_cfft_f32(
Sergeev 1:9b1df0b2507d 7 const arm_cfft_instance_f32 * S,
Sergeev 1:9b1df0b2507d 8 float32_t * p1,
Sergeev 1:9b1df0b2507d 9 uint8_t ifftFlag,
Sergeev 1:9b1df0b2507d 10 uint8_t bitReverseFlag);
Sergeev 0:b712470aea1d 11
Sergeev 5:173ae8477ae1 12 Serial pc(USBTX, USBRX);//:D ПРИВЕТ!!!!
Sergeev 5:173ae8477ae1 13
Sergeev 5:173ae8477ae1 14
Sergeev 1:9b1df0b2507d 15
Sergeev 1:9b1df0b2507d 16 AnalogIn left(A2);
Sergeev 1:9b1df0b2507d 17 AnalogIn right(A3);
Sergeev 5:173ae8477ae1 18 AnalogIn center(A3);
Sergeev 1:9b1df0b2507d 19
Sergeev 1:9b1df0b2507d 20 int SAMPLE_RATE_HZ = 40000; // Sample rate of the audio in hertz.
Sergeev 5:173ae8477ae1 21 //const int FFT_SIZE = 16; // Size of the FFT.
Sergeev 5:173ae8477ae1 22 const int FFT_SIZE = 1024; // Size of the FFT.
Sergeev 1:9b1df0b2507d 23
Sergeev 1:9b1df0b2507d 24 const static arm_cfft_instance_f32 *S;
Sergeev 3:1f56b8f439a1 25 //static arm_cfft_radix2_instance_f32 *S;
Sergeev 1:9b1df0b2507d 26 Ticker samplingTimer;
Sergeev 1:9b1df0b2507d 27 float samples[FFT_SIZE*2];
Sergeev 5:173ae8477ae1 28 float samples2[FFT_SIZE*2];
Sergeev 5:173ae8477ae1 29 float samples3[FFT_SIZE*2];
Sergeev 5:173ae8477ae1 30
Sergeev 1:9b1df0b2507d 31 float magnitudes[FFT_SIZE];
Sergeev 5:173ae8477ae1 32 float magnitudes2[FFT_SIZE];
Sergeev 5:173ae8477ae1 33 float magnitudes3[FFT_SIZE];
Sergeev 1:9b1df0b2507d 34 int sampleCounter = 0;
Sergeev 5:173ae8477ae1 35
Sergeev 1:9b1df0b2507d 36
Sergeev 1:9b1df0b2507d 37 void samplingCallback()
Sergeev 3:1f56b8f439a1 38 {
Sergeev 1:9b1df0b2507d 39 // Read from the ADC and store the sample data
Sergeev 3:1f56b8f439a1 40 samples[sampleCounter] = 1000*left.read();
Sergeev 5:173ae8477ae1 41 samples2[sampleCounter] = 1000*left.read();
Sergeev 5:173ae8477ae1 42 samples3[sampleCounter] = 1000*left.read();
Sergeev 1:9b1df0b2507d 43 // Complex FFT functions require a coefficient for the imaginary part of the input.
Sergeev 1:9b1df0b2507d 44 // Since we only have real data, set this coefficient to zero.
Sergeev 1:9b1df0b2507d 45 samples[sampleCounter+1] = 0.0;
Sergeev 5:173ae8477ae1 46 samples2[sampleCounter+1] = 0.0;
Sergeev 5:173ae8477ae1 47 samples3[sampleCounter+1] = 0.0;
Sergeev 1:9b1df0b2507d 48 // Update sample buffer position and stop after the buffer is filled
Sergeev 3:1f56b8f439a1 49
Sergeev 1:9b1df0b2507d 50 sampleCounter += 2;
Sergeev 2:707eb5bc5dfc 51
Sergeev 1:9b1df0b2507d 52 if (sampleCounter >= FFT_SIZE*2) {
Sergeev 1:9b1df0b2507d 53 samplingTimer.detach();
Sergeev 1:9b1df0b2507d 54 }
Sergeev 1:9b1df0b2507d 55 }
Sergeev 1:9b1df0b2507d 56
Sergeev 1:9b1df0b2507d 57 void samplingBegin()
Sergeev 1:9b1df0b2507d 58 {
Sergeev 1:9b1df0b2507d 59 // Reset sample buffer position and start callback at necessary rate.
Sergeev 1:9b1df0b2507d 60 sampleCounter = 0;
Sergeev 3:1f56b8f439a1 61 samplingTimer.attach_us(&samplingCallback, (float)(1000000/SAMPLE_RATE_HZ));
Sergeev 1:9b1df0b2507d 62 }
Sergeev 1:9b1df0b2507d 63
Sergeev 1:9b1df0b2507d 64 bool samplingIsDone()
Sergeev 1:9b1df0b2507d 65 {
Sergeev 1:9b1df0b2507d 66 return sampleCounter >= FFT_SIZE*2;
Sergeev 0:b712470aea1d 67 }
Sergeev 1:9b1df0b2507d 68
Sergeev 1:9b1df0b2507d 69 int main()
Sergeev 0:b712470aea1d 70 {
Sergeev 1:9b1df0b2507d 71 // Set up serial port.
Sergeev 3:1f56b8f439a1 72 //pc.baud (38400);
Sergeev 1:9b1df0b2507d 73
Sergeev 1:9b1df0b2507d 74 // Init arm_ccft_32
Sergeev 1:9b1df0b2507d 75 switch (FFT_SIZE)
Sergeev 1:9b1df0b2507d 76 {
Sergeev 1:9b1df0b2507d 77 case 16:
Sergeev 1:9b1df0b2507d 78 S = & arm_cfft_sR_f32_len16;
Sergeev 1:9b1df0b2507d 79 break;
Sergeev 1:9b1df0b2507d 80 case 32:
Sergeev 1:9b1df0b2507d 81 S = & arm_cfft_sR_f32_len32;
Sergeev 1:9b1df0b2507d 82 break;
Sergeev 1:9b1df0b2507d 83 case 64:
Sergeev 1:9b1df0b2507d 84 S = & arm_cfft_sR_f32_len64;
Sergeev 1:9b1df0b2507d 85 break;
Sergeev 1:9b1df0b2507d 86 case 128:
Sergeev 1:9b1df0b2507d 87 S = & arm_cfft_sR_f32_len128;
Sergeev 1:9b1df0b2507d 88 break;
Sergeev 1:9b1df0b2507d 89 case 256:
Sergeev 1:9b1df0b2507d 90 S = & arm_cfft_sR_f32_len256;
Sergeev 1:9b1df0b2507d 91 break;
Sergeev 1:9b1df0b2507d 92 case 512:
Sergeev 1:9b1df0b2507d 93 S = & arm_cfft_sR_f32_len512;
Sergeev 1:9b1df0b2507d 94 break;
Sergeev 1:9b1df0b2507d 95 case 1024:
Sergeev 1:9b1df0b2507d 96 S = & arm_cfft_sR_f32_len1024;
Sergeev 1:9b1df0b2507d 97 break;
Sergeev 1:9b1df0b2507d 98 case 2048:
Sergeev 1:9b1df0b2507d 99 S = & arm_cfft_sR_f32_len2048;
Sergeev 1:9b1df0b2507d 100 break;
Sergeev 1:9b1df0b2507d 101 case 4096:
Sergeev 1:9b1df0b2507d 102 S = & arm_cfft_sR_f32_len4096;
Sergeev 1:9b1df0b2507d 103 break;
Sergeev 1:9b1df0b2507d 104 }
Sergeev 1:9b1df0b2507d 105 float maxValue = 0.0f;
Sergeev 5:173ae8477ae1 106 float maxValue2 = 0.0f;
Sergeev 5:173ae8477ae1 107 float maxValue3 = 0.0f;
Sergeev 5:173ae8477ae1 108
Sergeev 1:9b1df0b2507d 109 unsigned int testIndex = 0;
Sergeev 5:173ae8477ae1 110 unsigned int testIndex2 = 0;
Sergeev 5:173ae8477ae1 111 unsigned int testIndex3 = 0;
Sergeev 1:9b1df0b2507d 112
Sergeev 1:9b1df0b2507d 113 // Begin sampling audio
Sergeev 1:9b1df0b2507d 114 samplingBegin();
Sergeev 1:9b1df0b2507d 115
Sergeev 1:9b1df0b2507d 116 while(1)
Sergeev 1:9b1df0b2507d 117 {
Sergeev 1:9b1df0b2507d 118 // Calculate FFT if a full sample is available.
Sergeev 1:9b1df0b2507d 119 if (samplingIsDone())
Sergeev 3:1f56b8f439a1 120 {
Sergeev 1:9b1df0b2507d 121 // Run FFT on sample data.
Sergeev 1:9b1df0b2507d 122 //arm_cfft_radix2_f32(arm_cfft_radix2_instance_f32*S, samples);
Sergeev 1:9b1df0b2507d 123 arm_cfft_f32(S, samples, 0, 1);
Sergeev 5:173ae8477ae1 124 arm_cfft_f32(S, samples2, 0, 1);
Sergeev 5:173ae8477ae1 125 arm_cfft_f32(S, samples3, 0, 1);
Sergeev 5:173ae8477ae1 126 samples[0]=0;
Sergeev 5:173ae8477ae1 127 samples2[0]=0;
Sergeev 5:173ae8477ae1 128 samples3[0]=0;
Sergeev 5:173ae8477ae1 129
Sergeev 3:1f56b8f439a1 130 /* Initialize the CFFT/CIFFT module */
Sergeev 3:1f56b8f439a1 131 //arm_cfft_radix2_init_f32(S, 128, 0, 1);
Sergeev 3:1f56b8f439a1 132 //arm_cfft_radix2_f32(S, samples);
Sergeev 5:173ae8477ae1 133 //for(int i = 0;i < FFT_SIZE*2;++i)
Sergeev 5:173ae8477ae1 134 // pc.printf(" Samples[%d]: %8.2f ",i,samples[i]);
Sergeev 5:173ae8477ae1 135 //pc.printf("\r\n");
Sergeev 1:9b1df0b2507d 136 // Calculate magnitude of complex numbers output by the FFT.
Sergeev 1:9b1df0b2507d 137 arm_cmplx_mag_f32(samples, magnitudes, FFT_SIZE);
Sergeev 5:173ae8477ae1 138 arm_cmplx_mag_f32(samples2, magnitudes2, FFT_SIZE);
Sergeev 5:173ae8477ae1 139 arm_cmplx_mag_f32(samples3, magnitudes3, FFT_SIZE);
Sergeev 5:173ae8477ae1 140
Sergeev 5:173ae8477ae1 141 //for(int i = 0;i < FFT_SIZE;++i)
Sergeev 5:173ae8477ae1 142 // pc.printf(" Magnitude: %d = %8.2f ;", i, magnitudes[i]);
Sergeev 5:173ae8477ae1 143 //pc.printf(" \r\n");
Sergeev 1:9b1df0b2507d 144 arm_max_f32(magnitudes, FFT_SIZE, &maxValue, &testIndex);
Sergeev 5:173ae8477ae1 145 arm_max_f32(magnitudes2, FFT_SIZE, &maxValue2, &testIndex2);
Sergeev 5:173ae8477ae1 146 arm_max_f32(magnitudes3, FFT_SIZE, &maxValue3, &testIndex3);
Sergeev 5:173ae8477ae1 147 pc.printf(" MAX value at magnitudes 1[%d] : %+8.2f\r\n", testIndex, maxValue);//Я НЯШКА А АНЯ ВЛАСЮК КОЗА:DDDDDDDDDDDDD
Sergeev 5:173ae8477ae1 148 pc.printf(" MAX value at magnitudes 2[%d] : %+8.2f\r\n", testIndex2, maxValue2);
Sergeev 5:173ae8477ae1 149 pc.printf(" MAX value at magnitudes 3[%d] : %+8.2f\r\n", testIndex3, maxValue3);
Sergeev 5:173ae8477ae1 150
Sergeev 5:173ae8477ae1 151 double delta_t = 0;
Sergeev 5:173ae8477ae1 152 if ((testIndex < testIndex2) and (testIndex < testIndex3) and (testIndex2 < testIndex3))
Sergeev 5:173ae8477ae1 153 delta_t = (testIndex3 - testIndex)*0.000025;
Sergeev 5:173ae8477ae1 154 else if ((testIndex < testIndex2)and(testIndex < testIndex3)and(testIndex2 > testIndex3))
Sergeev 5:173ae8477ae1 155 delta_t = (testIndex2 - testIndex)*0.000025;
Sergeev 5:173ae8477ae1 156 else if ((testIndex2 < testIndex3)and(testIndex2 < testIndex)and(testIndex < testIndex3))
Sergeev 5:173ae8477ae1 157 delta_t = (testIndex3 - testIndex2)*0.000025;
Sergeev 5:173ae8477ae1 158 else if ((testIndex2 < testIndex3)and(testIndex2 < testIndex)and(testIndex > testIndex3))
Sergeev 5:173ae8477ae1 159 delta_t = (testIndex - testIndex3)*0.000025;
Sergeev 5:173ae8477ae1 160 else if (testIndex2 < testIndex)
Sergeev 5:173ae8477ae1 161 delta_t = (testIndex - testIndex3)*0.000025;
Sergeev 5:173ae8477ae1 162 else
Sergeev 5:173ae8477ae1 163 delta_t = (testIndex - testIndex2)*0.000025;
Sergeev 5:173ae8477ae1 164 double fi = asin(330*delta_t/0.33);
Sergeev 5:173ae8477ae1 165 pc.printf(" Angle is : %+8.2f\r\n", fi);
Sergeev 5:173ae8477ae1 166
Sergeev 1:9b1df0b2507d 167 // Wait for user confirmation to restart audio sampling.
Sergeev 1:9b1df0b2507d 168 //pc.getc();
Sergeev 1:9b1df0b2507d 169 wait(1);
Sergeev 1:9b1df0b2507d 170 samplingBegin();
Sergeev 1:9b1df0b2507d 171 }
Sergeev 1:9b1df0b2507d 172 }
Sergeev 5:173ae8477ae1 173 }
Sergeev 5:173ae8477ae1 174