Draw timing diagrams in javascript with wavedrom

Wavedrom is a fantastic way to mock up a timing diagram. A timing diagram shows a sequence of how several signals change with time. Usually the signals are wire-line, meaning that they are electrical signals flowing through a wire. They are usually used to represent wire-line protocols such as SPI, I2C etc.

The changes are represented by single letter markers showing the state of the signal at a step in the plot. Example is a high to low transition represented as ‘pl’ among other signal changes. It is useful in describing a system of signals and may help one gain insight as to the time-relationship between signals. A tutorial may be found here.

The image below is of a timing diagram I previously constructed and associated code below:

timing_diagram

{signal:
[
{name: 'Column shift SRCLK', wave:'l.p|p.|p.|p.|p.|p.|p.|p.|l...'},
{name: 'Column data SER', wave:'x5..5..5..5..5..5..5..5..x...', data: ['ROW 0','ROW 1','ROW 2','ROW 3','ROW 4','ROW 5','ROW 6','ROW 7']},
{name: 'Column latch RCK', wave:'l.....pl.pl.pl.pl.pl.pl.pl.pl',},
{name: 'Row clock RCLK', wave:'l....pl.pl.pl.pl.pl.pl.pl.pl.',},
{name: 'Row reset RST', wave:'L...........................p'},
],
config: { hscale: 1 },
head:{text:'LED matrix timestep control',},
foot:{text:''},
}

 

An overview of software defined radio

Wikipedia defines software radio as Software-defined radio (SDR) as a radio communication system where majority of the radio components such as amplifiers, mixers, filters among others have been implemented in software instead of hardware. This means that these part of a radio system are in software code instead of something physical.

To quickly understand how a software defined radio works, imagine the sound card on a computer. It converts sound from analog(continuous time) to discrete samples. After that, you can record(store) the sound samples in various formats such as .wav, .mp3 and so on. It also allows one to apply filters to the sound thus one can also mix sounds and perform many other effects. The act of applying the effects is called digital signal processing(DSP). So when you hear a DJ on the radio, he is doing nothing more than math on sound samples and you like the effects.

There is main drivers for the development of SDR:

  1. Digital signal processing(DSP). When you are applying filters to sound and so on, that is DSP. It is just applying mathematics to samples to realise an effect.
  2. Moore’s law– The growth of transistors that can be packaged in a chip at a given cost grows exponentially. This increases the computational speed, but of late this has hit diminishing returns so multi-core processing systems are more popular.

To further understand how a software defined radio works, let us look at how WIFI works based on its OSI (Open systems interconnect) model.

File:Osi-model.png - Wikipedia, the free encyclopedia

The OSI model

The physical layer defines our layer of interest when working in this. Examples of how this layer works:

  • In WIFI, the physical layer defines how the bits are converted to wireless signals from sender to receiver.
  • In ethernet, the physical layer defines how the bits are converted to electrical signals that travel down an ethernet cable between sender and receiver.
  • in USB, the physical layer defines how the bits are converted to and from USB signals that travel down a USB cable, from a host to device or vice versa.

The image below shows an example system block diagram of a software defined radio from XILINX:

https://i1.wp.com/www.ti.com/graphics/blockdiagram/blockdiagram_images/6091.gif

SDR system block diagram

The antenna is on the far left. The upper section is the input path and it defines the receive section of the system. It consists of an analog to digital converter(converts signals to bits). The lower section is the transmit section of the system(converts bits to signals).

THE FUN  STUFF

GitHub - mossmann/hackrf: low cost software radio platform

Hackrf SDR platform (1MHz-6GHz)

You can imagine you are an engineer who has been asked to develop a wireless system and you want to check that it works, how do you check wireless signals???

  • You either use a dedicated radio that  can tune to the wireless frequency you want. The radio will most likely convert the wireless signal to information. Example: If you want to check that you have a wifi signal, you take your phone and it gives you this information.
  • You can use a software defined radio that you can tune into the wireless frequency range and inspect that there is the desired signal.

Understanding how communication happens

shows a typical system diagram of a modern digital communications ...

The communication model

To use a software defined radio: Connect the SDR to your computer and tune  to your frequency of interest. Obtain the  modulation from the data and convert it into bits/information. This is done usually in C++ or Python using the software framework gnuradio companion.

Screenshot_2016-12-17_21-10-11

A GNU-radio wireless capture session

Then one saves a recording and writes a software decoder to decode the signal usually in python or C++.

Example 1: vehicle wireless key signal(Digital)

An example of this is below when one presses a wireless key to open a car, you want to check that it works.

keyfob_unfiltered

A spectrogram. The horizontal axis is frequency, vertical is time.

To clearly see the signal sent to the car to unlock, we need to zoom and filter the signal. The brighter the  parts of the image, the more power there is thus giving an indication of information being sent. The rest of the image is just background noise power.

We thus isolate the lower power section which represents background noise and we obtain our signal to the car.

keyfob_color_plot

As can be seen, It can be seen that the power shifts between two frequencies thus one frequency represents a binary 1 and the other represents a binary 0. This sequence of 1s and 0s is binary data.

If we remember our definition of the physical layer, The physical layer defines how bits are encoded into physical signals. In this case, this case a binary 1 is encoded into frequency F1 and binary zero is encoded into frequency F2. This encoding is called modulation. This specific modulation is called BFSK(Binary frequency shift keying). The carrier frequency is generated usually by an oscillator on the wireless key and modulated by the data.

NOTE: A software defined radio can even demodulate the signal to give you the actual message. It is not enough to use the above system to open cars as the wireless key signal is pseudorandom nature as a rolling key(constantly changing under an algorithm) is XOR’d with the message. Once the car system receives the message, it XOR’s the message with the key to obtain the actual command message from the wireless key. More details of this can be studied in stream ciphers in cryptography. It relies on synchronizing the receiver and transmitter in order not to xor with the wrong key.

Example 2: FM capture(Analog)

FM means frequency modulation, it encodes low frequency information such as voice on a higher frequency wave called a carrier signal. The carrier signals in Kenya are standardised by the Communications authority from 88MHz-108MHz.

Animation of audio, AM and FM signals

Frequency vs amplitude modulation

 

To inspect this on the local FM band, we tune and we check the power across the frequency bands to obtain the following signals which confirm Frequency modulation. I remember there was a talk show at the moment this was captured.

FM modulation on the FM frequency bands.

 

 

SDR is thus an important tool for wireless designers, wireless researchers and hobbyists. This concludes the overview of software defined radio(SDR).

A brief introduction to unit testing in C with unity

Unit testing is the practice of testing different components of a code where the different components can be taken as independent entities that only interact in order to realize full functionality.

Usually when developing embedded code there is code under test and production code. Production code is code that will be released while code under test is code that is being tested, prior to release or probably as an experimental feature that may never see the light of day.

An effect of not testing code is an unpredictable behavior given an unforeseen input by the author. An example of such is a technique called fuzzing where random input is fed to an executable or code until it fails. Such a scenario would then be used to exploit an unforeseen vulnerability. More details in this Chaos Communications congress talk.

So how do you protect against that which you do not know?

Enter Unity. Unity is a C framework that is composed of C macros that checks(asserts) whether a given code under a given stimuli(input), gives an expected output. It is usually a test of code behavior under different scenarios. The code therefore should be deterministic.

Unity is meant for code that is incrementally tested  along its development as opposed to the traditional code development cycle where code is tested after development. This ensures code is tested even before it is refactored. Usage of unity is basically composed of 3 parts:

  • Test code– Code that is used to test the code under development.
  • Test caseTest code that checks result of code execution against the expected behavior.(Example: Input X should give input Y. If not, test failed).
  • Test fixture– A set of test cases that does the same as above but under different scenarios. It is grouped under a single entity. (Example: The code should behave this way for inputs X,Y or Z)

A more detailed use case of unity shall be given in a later post.

Sync an external kicad project with a copy within a git repository

When I work on projects, I usually run into the following problem when working with PCB design and code together:

  • I set up a repository using git whose location is at X. This repository contains say, code.
  • I later use a separate design program such as Kicad and start a design project whose location in my computer is at Y.
  • I realize I would like to have my Kicad design files in the repository X for versioning purposes and to synchronize with my team members but I do not feel like changing the current location of my Kicad design directory from Y to X.

Procedure

  • Change working directory to git repository
cd /path/to/git/repo
  • Make an empty bash script file in the current directory named  sync.sh
touch sync.sh
  • Open the file with your fave text editor. I use GNU nano text editor to edit the file
nano kicad_sync.sh

Once the file is open for editing use the

 #Include interpreter path if not in the PATH variable #!/bin/bash #sync any design files that are out of folder source_1="/path/to/external/kicad/folder/Y" dest_1="/path/to/destination/folder/within/this/git/repo/X" mkdir --parents --verbose "$dest_1" cp --recursive --update --interactive --verbose "$source_1"* "$dest_1"

Explanation:

  • #!/bin/bash

    Include the interpreter program if it has not been included in the PATH environment variable.

  • source_1="/path/to/external/kicad/folder/Y"

    Stores the kicad source folder path into variable source_1

  • dest_1="/path/to/destination/folder/within/this/git/repo/X"

    Stores the destination folder path into variable dest_1

  • mkdir --parents --verbose "$dest_1"

The line above creates the directories including the parents if they do not exist. Example: ./design/Kicad may be a non-existent directory in the git folder but the command creates the parent folder /design/ first if it doesnt exist, then creates the folder KIcad after that.

cp --recursive --update --interactive --verbose "$source_1"* "$dest_1"

copy from source_1 all files and folders recursively to dest_1 only if they are updated/new and query  interactively.

  • Save and close the bash script file.
  • Now anytime I would make changes to the PCB design and I would like to put those changes in the git project all I have to do is:
    sh kicad_sync.sh

    The file in the git folder is now updated and I can now then continue with my normal git workflow. Remember to add the local kicad project folder to the staging area then commit.

Record and share terminal sessions with asciinema

Asciinema is the perfect tool to share terminal sessions and it is one that we regularly use at Bitsoko to share a terminal workflow or record terminal behaviour.

You can find the asciinema website here.

Asciinema package is available in the Ubuntu repository in Ubuntu 16.04. It may not be bleeding edge as with any repository package but it works well out of the box.

To install asciinema on Ubuntu using the Ubuntu repos, run the following:

sudo apt update
sudo apt upgrade
 sudo apt install asciinema 

To start a record session:

asciinema rec

To exit from a record session:

exit

A sample record session of mine can be found here.

Lossy image color space reduction with pngquant

As a website maintainer one usually needs to reduce the average image file size on a website in order to save bandwidth. A tool I just started using on linux is pngquant to reduce the size of my site’s PNG images prior to upload. This is based on a Nairobi linux users group thread.  Pngquant also is available for windows and mac on their website.

In the case of JPEGs I manually convert them to PNGs using gimp or use the convert tool in a one line command to automatically convert all JPEG images to PNGs in a given folder as in this post here.

 convert *.jpg *.png 

Install pngquant and via terminal. I use Ubuntu 16.04.

sudo apt install pngquant

I then use pngquant to reduce the all PNG file sizes in the folder as detailed here.

 pngquant --speed 1 --verbose *.png 

screenshot-from-2017-02-03-18-47-15-fs8

The input image file from a previous post is thus reduced from 47kB to 23kB. Here is a direct visual comparison below:

original(47kB)

screenshot-from-2017-01-30-22-05-17

output(23kB)

screenshot-from-2017-01-30-22-05-17-fs8