Tutorial 1 - Matlab & PyBench

6
Tutorial 1 Slide 1 PYKC 16 Jan 2020 EA2.3 - Electronics 2 DE2.3 Electronics 2 for Design Engineers Tutorial 1 Lab 1 - PyBench Board & Spectral Analysis Peter Cheung Dyson School of Design Engineering URL: www.ee.ic.ac.uk/pcheung/teaching/DE2_EE/ E-mail: [email protected] Tutorial 1 Slide 2 PYKC 16 Jan 2020 EA2.3 - Electronics 2 Lab 0 - Ex 1: The sine_gen function Tutorial 1 Slide 3 PYKC 16 Jan 2020 EA2.3 - Electronics 2 Lab 0 - Ex 2: The plot_spec function Tutorial 1 Slide 4 PYKC 16 Jan 2020 EA2.3 - Electronics 2 Lab 0 - Ex 3: Two tones (fs = 8192, T = 1) s1 = 440Hz sine at 1V s2 = 1kHz sine at 0.5V + sig = s1 + s2 plot_spec (sig, 8192)

Transcript of Tutorial 1 - Matlab & PyBench

Page 1: Tutorial 1 - Matlab & PyBench

Tutorial 1 Slide 1PYKC 16 Jan 2020 EA2.3 - Electronics 2

DE2.3 Electronics 2 for Design Engineers

Tutorial 1

Lab 1 - PyBench Board & Spectral Analysis

Peter CheungDyson School of Design Engineering

URL: www.ee.ic.ac.uk/pcheung/teaching/DE2_EE/E-mail: [email protected]

Tutorial 1 Slide 2PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 0 - Ex 1: The sine_gen function

Tutorial 1 Slide 3PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 0 - Ex 2: The plot_spec function

Tutorial 1 Slide 4PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 0 - Ex 3: Two tones (fs = 8192, T = 1)

◆ s1 = 440Hz sine at 1V ◆ s2 = 1kHz sine at 0.5V

+

◆ sig = s1 + s2 ◆ plot_spec (sig, 8192)

Page 2: Tutorial 1 - Matlab & PyBench

Tutorial 1 Slide 5PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 0 - Ex 4: Two tones + noisy

◆ Noisy = sig + randn(size(sig));

◆ plot_spec (noisy, 8192)

Tutorial 1 Slide 6PYKC 16 Jan 2020 EA2.3 - Electronics 2

What’s on the Board

Pyboard

Motor Driver

12V to 5V converter

Bluetooth to UART

potentiometer

a_out

a_in Organic LED128 x 64 display

Inertial Measurement

Unit (IMU)

Microphone & Amplifier

RED = 5VBLACK = GNDBLUE = 3.3V

Tutorial 1 Slide 7PYKC 16 Jan 2020 EA2.3 - Electronics 2

How PyBench works?

Pyboard runningpybench.py

PC running Matlab(with class pybench.m)

USB linkanalogue in

analogue out PyFlash MicroSD

Mic Amp

◆ Create an object pb for the PyBench Board:

◆ Control the Board via “methods”, e.g. pb.set_max_v (2.5).

◆ PC sends three bytes to PyBench board via USB link as serial data. First byte is a command character. In this case, ‘X’, followed by the value of voltage as two bytes. First byte is int (4096 * (v/3.3) / 256), and second byte is int (4096 * (v/3.3)) mod 256.

◆ All along, PyBoard is running a Python program (pybench_main.py) listening for a command. The BLUE LED is ON in this state. Waiting for a event such as a character to arrive is known as “polling”.

◆ When it receives the command (3 bytes), it call the pybench class method to set the maximum voltage.

Tutorial 1 Slide 8PYKC 16 Jan 2020 EA2.3 - Electronics 2

What are stored in the MicroSD card?

boot.py main;py pybench_main

pybench_test

pybench.py

oled_938.py

mpu6050.py

driver.py

Page 3: Tutorial 1 - Matlab & PyBench

Tutorial 1 Slide 9PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 1 – Generate and Capture Signals

Tutorial 1 Slide 10PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 1 – Spectrum of square wave (440Hz)

a1 = 1.6

a3 = 0.53

a5 = 0.32

a7 = 0.23

440Hz A=2.5V Spurious components

(why?)

Tutorial 1 Slide 11PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 1 – Spectrum of square wave (400Hz)

400Hz A=2.5V Spurious components

disappeared

Tutorial 1 Slide 12PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 1 – Fourier Coefficients of square wave

Take fundamental f0 Add 3f0 Add 5f0

Add 7f0

… carry on indefinitely …

Page 4: Tutorial 1 - Matlab & PyBench

Tutorial 1 Slide 13PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 1 – Fourier Coefficients of common waveforms

a0 = dc value (i.e zero frquency)an = real part of magnitude of nth harmonicbn = imaginary part of magnitude of nth harmonic

Tutorial 1 Slide 14PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 1 – plot_spec_full ( )

Tutorial 1 Slide 15PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 2 – Microphone signal

tuning fork – 1000Hz

Tutorial 1 Slide 16PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 2 – Demonstrate spectral folding (aliasing)

3000Hz

3900Hz

4300Hz foldedto 3700Hz

5000Hz foldedto 3000Hz

Increasing frequency

Page 5: Tutorial 1 - Matlab & PyBench

Tutorial 1 Slide 17PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 2 – Effect of changing N – no of samples to analyse

N = 1000

N = 100

• Increase N improves the frequency resolution (Df) of the spectrum

• Df = "# $∗&'• Ts = sampling period = 1/fs

Tutorial 1 Slide 18PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 3 – Magnitude in dB

Tutorial 1 Slide 19PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 3 – Windowing effect

1000 Hz sampled at 8kHzN = 1000 or 125 exact cycles

1100 Hz sampled at 8kHzN = 1000 or 137.5 cycles

Tutorial 1 Slide 20PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 3 – Rectangular vs Hamming Window

Rectangular window

Hamming window

Page 6: Tutorial 1 - Matlab & PyBench

Tutorial 1 Slide 21PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 4 – Analyse beat of drum beats (1)

Signal x(t)

Energy of signal

Tutorial 1 Slide 22PYKC 16 Jan 2020 EA2.3 - Electronics 2

Lab 1 Exercise 4 – Analyse beat of drum beats (2)

Energy vs time

Spectrum of energy

FFT