Skip to content

Instantly share code, notes, and snippets.

View jakelevi1996's full-sized avatar

Jake Levi jakelevi1996

View GitHub Profile
@jakelevi1996
jakelevi1996 / Linked lists in C.md
Last active January 11, 2020 14:07
Linked lists in C

Linked lists in C

A linked list is a custom data structure which can be created in C and other programming languages, designed for lists which may need to have elements added or removed from them at runtime, without knowing how big the list will need to be when it is initialised. A linked list is similar to an array, but has some important differences:

  • When an array is initialised, the size of the array must be declared, and all of the memory for the array is allocated with a single command. With a linked list, memory is allocated/freed dynamically every time a new element is added or removed from the list
  • Once an array is created, there is no safe and efficient way of adding or removing elements from the array, whereas a linked list is designed to have elements added and removed at runtime
  • When an array is created, all the memory is allocated in a single command, and the memory addresses for each element are created next to each other, making it easy to access the element at any position in the lis
@jakelevi1996
jakelevi1996 / Multiple source files in C.md
Last active May 16, 2026 03:08
Multiple source files in C.

Multiple source files in C

This Gist presents an introduction to a few different ways of working with multiple source files in C, including:

  1. Simple .c source files and .h header files compiled into a .exe
  2. First compiling into .o object files, and then linking together
  3. Makefiles
  4. Statically linked libraries
  5. Dynamically linked libraries

The concepts are demonstrated using gcc (tdm64-1) 5.1.0 on Windows 8.1, using 3 C source files (linkedlist.c, listprimes.c and main.c) and 2 header files (linkedlist.h and listprimes.h), all of which are included at the end. It is assumed that all the source files are saved in a single directory, and any terminal commands are entered in a terminal window open in that same directory.

@jakelevi1996
jakelevi1996 / Pipes between processes in C.md
Last active January 11, 2020 14:16
Pipes between processes in C

Pipes between processes in C

File redirection operators are useful command line tools; for example, > can be used to redirect the standard output of a program to a text file, < can be used to redirect a text file to the standard input of a program, and 2> can be used to redirect the standard error of a program. Another useful command line operator is the | (pipe) operator, which is used to redirect the standard output from one program to the standard input of another program; for example, take the following bash command, in which the standard output from ps (process summary) is being used as the standard input to grep (global regular-expression print):

$ ps | grep cons1
     1433       1    1433       4932  cons1     197610 22:59:39 /usr/bin/bash
     1493    1433    1493       7932  cons1     197610 23:27:11 /usr/bin/ps
@jakelevi1996
jakelevi1996 / Plotting with matplotlib.md
Last active September 14, 2020 17:03
Plotting with matplotlib

Plotting with matplotlib

Instead of writing the same plotting code using plt.plot again and again in different modules/scripts, here is a simple function which plots data and formats, saves and closes the figure; this function can be used and modified instead of writing the same lines of code again and again in different repositories. Following this simple function are some scripts which demonstrate some of the fancier features of pyplot; the resulting images are shown below the code.

Simple plotting function

import numpy as np
import matplotlib.pyplot as plt
@jakelevi1996
jakelevi1996 / Digital audio equalisation using `scipy.signal`.md
Last active January 17, 2020 09:44
Digital audio equalisation using `scipy.signal`

Digital audio equalisation using scipy.signal

The book "Digital Signal Processing - Fundamentals and applications" (Li Tan, Jean Jiang, 2nd Edition) contains an example in chapter 8, section 5 of a digital audio equaliser. Unfortunately, this example is written in Matlab; here the same concepts are implemented in a Python script, using scipy.signal. The script uses cascaded second-order section (SOS) filters; a SOS filter is made by taking a high-order IIR filter and splitting it into several 2nd-order IIR filters (AKA digital biquad filters) which are applied to an input signal sequentially. The motivation for using SOS filters is that high-order IIR filters can contain both very large and very small filter coefficients, and using such a filter can be numerically unstable when using floating-point arithmetic. Below is the Python script which implements a simple digital audio equaliser, foll

@jakelevi1996
jakelevi1996 / Comparing DFT vs FFT.md
Last active October 31, 2024 11:51
Comparing DFT vs FFT

Comparing DFT vs FFT

Below is a Python script which contains functions for computing the naive Discrete Fourier Transform (DFT), a recursive radix-2 Fast Fourier Transform (FFT) algorithm, and an in-place FFT radix-2 algorithm.

TODO: add timing information for FFT in-place; add timeplot output; compare outputs with FFT in-place; reorganise structure of Gist

import numpy as np
import matplotlib.pyplot as plt
from time import perf_counter
@jakelevi1996
jakelevi1996 / Downloading files in Python.md
Created January 31, 2020 09:40
Downloading files in Python

Downloading files in Python

This is a simple script to check if a particular file exists in the same directory as the Python script; if the file doesn't exist, it is downloaded.

getpanda.py

import os, urllib.request

# Change to directory of current script (in case running from another location)
@jakelevi1996
jakelevi1996 / numpy PIL interface.md
Last active February 7, 2020 11:52
numpy/PIL interface

numpy/PIL interface

This gist provides a simple example of how to convert an image back and forth between a numpy.ndarray and a PIL.Image.Image, resize the image using Image.resize, and display the image using each representation.

PIL stands for Python Imaging Library, and can be installed with the command pip install pillow (pillow is a fork of the original PIL module which has been updated for Python 3). This blog post provides more useful examples on how to perform other image-processing operations using pillow.

NB this Gist assumes that panda.jpg is present in the same directory as the `pil_te

@jakelevi1996
jakelevi1996 / Notes on bash.md
Last active February 20, 2023 14:18
Notes on bash