Created
April 19, 2016 06:38
-
-
Save philkuz/c7d283bedfc686afb4ba155b4bb1e381 to your computer and use it in GitHub Desktop.
Classify for the MSP430 project SIXT33N
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* classify.ino | |
** Processing Integration sketch for SIXT33N Music version | |
** | |
** EE16B Spring 2016 | |
** Emily Naviasky & Nathaniel Mailoa | |
** | |
*/ | |
#include "./fix_fft.c" | |
#define MIC_INPUT P6_0 | |
//data array and index pointer | |
int8_t re[128]={0}; | |
int8_t im[128]={0}; | |
volatile int re_pointer = 0; | |
// Loudness variables | |
#define SIZE 128 | |
float sum = 0.0; | |
int16_t loudness = 0; | |
//timer period | |
float timer_ms = 0.35; | |
int i = 0; | |
//past pca values | |
float pca1 = 0; | |
float pca2 = 0; | |
int pca_count = 0; | |
/*---------------*/ | |
/* CODE BLOCK A1 */ | |
/*---------------*/ | |
float pca_vec[SIZE] = {0.36961871, -0.02157957, -0.06643426, -0.0472582 , -0.03475233, | |
-0.03053084, -0.0357888 , -0.0319841 , -0.0373197 , -0.02290277, | |
-0.02589317, -0.03865687, -0.02433723, -0.02404461, -0.03868492, | |
-0.02350191, -0.04277367, -0.02927904, -0.02708781, -0.02192353, | |
-0.02994287, -0.06783646, -0.04372428, -0.05410791, -0.07996994, | |
-0.18553457, -0.13793853, -0.10384696, -0.18762848, -0.15174473, | |
-0.11019488, -0.17577634, -0.2786608 , -0.14489447, -0.13228256, | |
-0.07352004, -0.06066424, -0.07704905, -0.1140658 , -0.06518871, | |
-0.03758684, -0.04655808, -0.09593589, -0.09474074, -0.05546122, | |
-0.04073478, -0.03469756, -0.04917424, -0.06347584, -0.04784789, | |
-0.08146853, -0.07289467, -0.05139586, -0.0437993 , -0.04043633, | |
-0.0400711 , -0.0561905 , -0.05682905, -0.04342455, -0.03479911, | |
-0.03157138, -0.03812093, -0.05048729, -0.08528685, -0.07269989, | |
-0.07346596, -0.04162662, -0.03770049, -0.03070203, -0.03132942, | |
-0.04247361, -0.0557713 , -0.05758171, -0.03686646, -0.03964496, | |
-0.04290055, -0.05275807, -0.07490841, -0.07958092, -0.0473259 , | |
-0.06697581, -0.04989068, -0.03139145, -0.03778408, -0.05475513, | |
-0.0958019 , -0.09641158, -0.04644897, -0.03746413, -0.06431263, | |
-0.11370408, -0.07641059, -0.06273487, -0.07449759, -0.13609858, | |
-0.15616847, -0.2786608 , -0.16160025, -0.11080122, -0.15096024, | |
-0.18578613, -0.10231493, -0.1362872 , -0.18728865, -0.07873092, | |
-0.05390055, -0.04539437, -0.06843834, -0.03162144, -0.02406253, | |
-0.03186428, -0.03058804, -0.03984903, -0.02316359, -0.03600624, | |
-0.0232271 , -0.02653836, -0.03798149, -0.02847182, -0.02631873, | |
-0.03442672, -0.03439904, -0.0340884 , -0.03316588, -0.04039763, | |
-0.04475666, -0.05986868, -0.01600604}; | |
float pca_mean[SIZE] = {34.46929134, 3.3707584 , 3.08001093, 5.0826622 , | |
1.60044448, 1.79059817, 1.78523682, 1.5166166 , | |
1.32300447, 1.35752607, 1.36712058, 1.54703564, | |
1.33250719, 1.36035817, 1.40240013, 1.45619499, | |
1.23200691, 1.55025895, 1.36007019, 1.46405677, | |
1.38501608, 1.92792947, 1.57599728, 1.73404366, | |
1.8871999 , 2.92976823, 2.33234112, 1.89245232, | |
2.55869569, 2.22194983, 2.15373162, 2.17644556, | |
2.90070407, 2.21108739, 2.36213212, 1.49028508, | |
1.3273623 , 1.691877 , 2.5025428 , 1.72285417, | |
1.48961834, 1.41820489, 1.89885704, 1.8607301 , | |
1.37216697, 1.41685939, 1.23410389, 1.39243199, | |
1.27910646, 1.3550214 , 1.60314305, 1.59652988, | |
1.38613404, 1.3469953 , 1.21923294, 1.22976891, | |
1.31878859, 1.3961074 , 1.21875092, 1.12668629, | |
1.01179966, 1.17968176, 1.03313346, 1.3001137 , | |
1.01417323, 1.38531879, 1.14180217, 1.18117876, | |
1.02306567, 1.12290801, 1.23446107, 1.42616299, | |
1.24018307, 1.16994273, 1.19114701, 1.37093289, | |
1.39264708, 1.5696458 , 1.55232533, 1.33635819, | |
1.22876266, 1.34241884, 1.24318354, 1.38559406, | |
1.38279895, 1.83603292, 1.8639242 , 1.39973509, | |
1.52667766, 1.67743329, 2.52320923, 1.69342518, | |
1.32488286, 1.47595949, 2.38470799, 2.05568551, | |
2.90070407, 2.32178311, 2.14175483, 2.2348224 , | |
2.54987861, 1.86163036, 2.33139452, 3.00581412, | |
1.8646359 , 1.74405433, 1.60662148, 2.02518438, | |
1.39472613, 1.49972857, 1.38313568, 1.61398369, | |
1.34045559, 1.4230733 , 1.43932994, 1.36491934, | |
1.39503086, 1.54744278, 1.40614743, 1.45760556, | |
1.49576406, 1.54850689, 1.84227425, 1.88801913, | |
1.7815055 , 5.16475932, 3.56839305, 4.30924351}; | |
/*----------------------*/ | |
/* END OF CODE BLOCK A1 */ | |
/*----------------------*/ | |
void setup() | |
{ | |
//microphone ADC | |
pinMode(MIC_INPUT, INPUT); | |
pinMode(P6_1, INPUT); | |
pinMode(P6_5, INPUT); | |
//recording light | |
pinMode(RED_LED, OUTPUT); | |
pinMode(GREEN_LED, OUTPUT); | |
re_pointer = 0; | |
setTimer(); | |
reset_blinker(); | |
Serial.begin(38400); | |
} | |
void loop() | |
{ | |
// This loop is entered every time the | |
// data collection buffer is filled and | |
// code is ready to start a new FFT | |
if(re_pointer == 128){ | |
// Loudness calculation (with filter) | |
// loudness ranges from 2500 to 4500 | |
sum = 0; | |
for (int i = 0; i < SIZE; i++){ | |
sum += abs(re[i]); | |
} | |
loudness = loudness*0.5 + sum*0.5; | |
Serial.print("loudness: "); | |
Serial.println(loudness); | |
// make sure imaginary is set to zero | |
memset(im, 0, sizeof(im)); | |
// take a 128-point fft | |
fix_fft(re,im,7,0); | |
/*--------------*/ | |
/* CODE BLOCK B */ | |
/*--------------*/ | |
// Modify this section to add your | |
// data processing and decision making | |
// STUDENT CODE!!!!!!!!! | |
float pca_val = 0; | |
for (i=0; i<128; i++){ | |
pca_val += (norm(re[i], im[i]) - pca_mean[i]) * pca_vec[i]; | |
} | |
// Classification | |
float threshold = 0; | |
if (avg_pca(pca_val) > threshold) { | |
Serial.println("classical"); | |
digitalWrite(GREEN_LED, HIGH); | |
digitalWrite(RED_LED, LOW); | |
} else { | |
Serial.println("rap"); | |
digitalWrite(GREEN_LED, LOW); | |
digitalWrite(RED_LED, HIGH); | |
} | |
/*---------------------*/ | |
/* END OF CODE BLOCK B */ | |
/*---------------------*/ | |
re_pointer = 0; | |
} | |
} | |
/*----------------*/ | |
/* CODE BLOCK 420 */ | |
/*----------------*/ | |
float avg_pca(float curpca) { | |
float avg = curpca; | |
if (pca_count > 2) { | |
avg = (curpca + pca1 + pca2) / 3; | |
} | |
pca_count += 1; | |
pca2 = pca1; | |
pca1 = curpca; | |
return avg; | |
} | |
float norm(float x, float y) { | |
float x2 = x * x; | |
float y2 = y * y; | |
return sqrt(x2 + y2); | |
} | |
/*-----------------------*/ | |
/* END OF CODE BLOCK 420 */ | |
/*-----------------------*/ | |
#pragma vector=TIMER0_A0_VECTOR // Timer A ISR | |
__interrupt void Timer1_A0_ISR( void ) | |
{ | |
if(re_pointer < 128){ | |
digitalWrite(RED_LED, HIGH); | |
re[re_pointer] = (analogRead(P6_0) >> 4) - 128; | |
re_pointer += 1; | |
} | |
} | |
void setTimer(){ | |
// set the timer based on 25MHz clock | |
TA0CCR0 = (unsigned int)(25000*timer_ms); | |
// enable interrupts for Timer A | |
TA0CCTL0 = CCIE; | |
__bis_SR_register(GIE); | |
TA0CTL = TASSEL_2 + MC_1 + TACLR + ID_0; | |
} | |
void reset_blinker(){ | |
pinMode(RED_LED, OUTPUT); | |
pinMode(GREEN_LED, OUTPUT); | |
digitalWrite(RED_LED, HIGH); | |
delay(100); | |
digitalWrite(RED_LED, LOW); | |
digitalWrite(GREEN_LED, HIGH); | |
delay(100); | |
digitalWrite(RED_LED, HIGH); | |
digitalWrite(GREEN_LED, LOW); | |
delay(100); | |
digitalWrite(RED_LED, LOW); | |
digitalWrite(GREEN_LED, HIGH); | |
delay(100); | |
digitalWrite(GREEN_LED, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment