Lecture 27: Homework 7 and Lab 4 10:10 Timeline 4 April: Distribution Homework 7 and Lab 4 16 April: Homework 7 due date 25 April: Lab 4 due date ATTENTION: THIS LAB HAS MORE COMPLEXITY THAN PREVIOUS LABS. YOU HAVE TO START EARLY. 10:12 Demonstration - Startup & Idle screen - Gameplay computer start - Gameplay player start - Gameplay player abort 10:15 Structure of the Homework and Lab There are two repositories and three documents to explain the assignment. 1/ Classroom github link for Homework 7 -> repo with starter files for Homework 7 2/ Classroom github link for Lab 4 -> repo with starter files for Lab 4 3/ ECE2534_HW7_Spring2018.pdf - Homework 7 assignment Document that explains HW7 4/ ECE2534 Lab 4 Specification Spring 2018.pdf Document that explains LAB4 5/ ECE 2534 Lab 4 HW 7 Design Doc Spring 2018.pdf Design Document with background information for HW7/ LAB4 Before you start either HW7 or LAB 4, read the Lab 4 HW 7 Design Doc completely. 10:18 Tic Tac Toe RELATED PROBLEM: HOMEWORK 7 PART 3 The Tic Tac Toe Game State can be described as follows: typedef enum {empty, cross, circle} tcellstate; tcellstate map[9]; In Lab 4, where one needs to keep track of the score, the game state is expanded with score as follows: typedef struct { tcellstate map[9]; int computerscore; int humanscore; } gamestate_t; The function of a game state is as follows: Game Game Logic Display Procedures Procedures \ / -- gamestate_t -- Game Logic procedures include: - checking if a move is valid 'play circle at position 5' - checking if there is a winner or tie - clearing the playing field Game Display procedures include: - Drawing the grid and game state - Highlighting a winning row/column/diagonal Computer game play can be from very simple to very sophisticated. - Very simple: play a random position at every turn - Very sophisticated: build a game gree - Medium sophisticated: use heuristics -> you can use any strategy, an a better-than-random strategy carries a bonus 10:25 Sound Generation RELATED PROBLEM: HOMEWORK 7 PART 2 - Each note is characterized by (frequency, duration) - Use buzzer and PWM to generate a frequency. - Use a software timer to generate a precise duration. Example tones: note_f4 F4 349.23 Hz How to generate a tone of 349.21 Hz? - Use Timer_A Put the counter in PWM mode, duty cycle 50% Timer period? CCR0 = SYSCLK / k / freq with SYSCLK = system clock k = divider freq = taken from table Duty cycle = 0.5 * Timer Period 10:30 Microphone RELATED PROBLEM: HOMEWORK 7 PART 1 Requirement for microphone samples: - They need to be taken at 8KHz - The signal must not be too loud or too weak General setup: Timer ISR ---> One ADC conversion ---> DSP Filter 8KHz The ADC samples are fed into a DSP processing chain. For Lab 4, the processing chain is a Goertzel Filter For Homework 7, the processing chain is a min/max tracker Typically, we left the filters run for N samples before evaluating their output. For Lab 4, N corresponds to a 50ms time window For Homework 7, N corresponds to a 500ms time window In Lab 4, microphone samples will need to be scaled to fixed-point numbers (<32,8>). We scale them to fractional samples between -1 and +1 as follows: Full ADC Eff Range Shifted Scaled Fractional <32, 8> =========================================================== 0x3FFF ............. 0x2FFF 0x1000 0x100 1 0x1FFF ------ 0x1FFF ------ 0x0 ------ 0x0 ------ 0 ............. 0x1000 0xF000 0xFF00 -1 0x0000 10:40 Goertzel Filters RELATED PROBLEM: HOMEWORK 7 PART 4 DTMF signals are defined as follows 1209 Hz 1336 Hz 1477 Hz ------------------------------- 697 Hz 1 2 3 770 Hz 4 5 6 852 Hz 7 8 9 To detect a key press, we will need bandpass filters: signal ---> filter for 695 Hz ---> power detector 1 ---> filter for 770 Hz ---> power detector 2 ---> filter for 852 Hz ---> power detector 3 ---> filter for 1209 Hz ---> power detector 4 ---> filter for 1336 Hz ---> power detector 5 ---> filter for 1477 Hz ---> power detector 6 A '1' key press then is detected by checking: (power_1 > THRESHOLD) && (power_4 > THRESHOLD) More sophisticated detection may be needed to prevent false positives, but that is for you to find out in lab 4. The power is computed by feeding N samples of the sampled signal into the powerbank. We take N to be 400 samples, which corresponds to 50ms of audio data. How do we build these filters? It turns out there is a very efficient construction to do this, and it's called a GOERTZL filter. x[n] ---> + -----> s[n] -----------+--> y[n] | | | | D | | | | +<-- c - s[n-1] --- e ---+ | | | D | | +-- -1 - s[n-2] s[n] = x[n] + c. s[n-1] - s[n-2] y[n] = s[n] + exp(-j.2.pi.k/N).s[n-1] k = N.fdetect/fsample c = 2.cos(2.pi.k/N) The power output of the filter is computed using the following expression: y[N] * y[N] = s[N-1] * s[N-1] + s[N-2] * s[N-2] – c * s[N-1] * s[N-2]; We need to implement this filter in FIXED POINT ARITHMETIC, because it uses fractional coefficients. Example. Assume we are using <32, 8> We detect a frequency of 695 Hz for a sample frequency of 8000 Hz Therefore c = 2.cos(2.pi.fdetect/fsample) c = 1.7093708 In <32,8>, c = int(256 * 1.7093708) = 437 So the goertzel filter is as follows. Given a sample from the ADC (scaled to <32,8>), we compute ResetGoertzel: sn1 = 0; sn2 = 0; FilterGoertzel: sn = xn + (362 * sn1) >> FRACBITS – sn2; sn2 = sn1; sn1 = sn; Finally, we need to compute the power output Power = (sn1 * sn1) >> FRACBITS + (sn2 * sn2) >> FRACBITS - (((362 * sn1) >> FRACBITS) * sn2) >> FRACBITS 10:58 Summary - Start with Homework 7 today 1/ Read the design document 2/ Do part 1: microphone peak detector 3/ Do part 2: music maker 4/ Do part 3: tic tac toe display 5/ Do part 4: goertzel filter - Start with Lab 4 early on, using components created in Homework 7