Quantum
Quest

Algorithms, Math, and Physics

Advanced syntax highlighting for Python & MATLAB/Octave

In the realm of software development and data analysis, clear and effective communication of code is paramount. This blog post explores the innovative techniques we’ve implemented to elevate syntax highlighting for Python and MATLAB/Octave, ensuring that your code not only performs well but also presents beautifully in documentation and blogs.

Python showcase


import numpy as np
import matplotlib.pyplot as plt

def spectral_analysis(data, sampling_rate):
    """
    Perform a spectral analysis of the input data using FFT.
    """
    data_fft = np.fft.fft(data)
    frequencies = np.fft.fftfreq(data.size, d=1/sampling_rate)

    plt.plot(frequencies, np.abs(data_fft))
    plt.title('Spectral Analysis')
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Amplitude')
    plt.show()

# Generate a sample signal with two frequencies
sampling_rate = 1000
time = np.arange(0, 1, 1/sampling_rate)
signal = np.sin(2 * np.pi * 50 * time) + np.sin(2 * np.pi * 120 * time)

spectral_analysis(signal, sampling_rate)

This Python example demonstrates a simple class structure, exception handling, and the use of functions. Such code snippets benefit greatly from clear syntax highlighting, aiding understanding and readability.

MATLAB/Octave showcase


function dydt = pendulum(t, y, b, c)
    %PENDULUM Function to model the differential equation of a pendulum
    dydt = [y(2); -b*y(2) - c*sin(y(1))];
end

% Parameters
b = 0.25; % Damping coefficient
c = 5.0;  % Gravity term

% Initial conditions
y0 = [pi - 0.1; 0]; % Initial angle and velocity

% Time span
tspan = [0 10];

% Solve ODE
[t, y] = ode45(@(t, y) pendulum(t, y, b, c), tspan, y0);

% Plot results
plot(t, y(:, 1));
title('Pendulum Motion');
xlabel('Time (seconds)');
ylabel('Angle (radians)');

The MATLAB/Octave example showcases matrix operations and function definitions within a script. Proper syntax highlighting brings clarity to these operations, enhancing the code’s educational value.

For more information and examples of the syntax highlight tool, please visit this link.