GPS Signal Generator
The example describes the operation of the gpsWaveformGenerator block, namely the various types of signals generated with its help (P-, L1C-, L2C- and L5-codes).
Introduction
GPS is a space—based radio navigation infrastructure consisting of a constellation of satellites transmitting navigation signals and a network of ground stations and satellite control stations for monitoring and controlling these satellites.
GPS satellites use three different frequencies for civilian applications: L1 at 1575.42 MHz, L2 at 1227.60 MHz, and L5 at 1176.45 MHz.
GPS uses five different civilian signals: coarse capture code (C/A) and precision code (P) in the legacy L1 band, upgraded civilian L1 code (L1C) in the L1 band, civilian medium code (CM) and civilian long code (CL) in the L2C band, and common-mode code (I5-code) and a quadrature code (Q5 code) in the L5 range.
The P-code
The P-code is a rangefinder code that uses the BPSK (Binary Phase Shift Keying) modulation method for transmission at L1 and L2 frequencies with a chip rate of 10.23 Mips/s. The higher chip speed of the P-code contributes to increased accuracy compared to the C/A-code.
The P-code is publicly available, but its encryption using the Y-code to form the P(Y)-code restricts its use for military purposes.
Below is a script for generating and displaying a P-code spectrogram.
SampleRate = 4 * 10.23e6 sets the sampling rate 40.92 MHz, i.e. 4 samples per P‑code chip. This satisfies Kotelnikov's theorem, since the width of the main lobe of the P‑code spectrum is 20.46 MHz (from -10.23 to +10.23 MHz), and allows you to see several side lobes.
navdata — a vector of two random bits (0 or 1) simulating a GPS navigation message (in reality, the data transfer rate is 50 bits/s, the P‑code is modulated by these bits via BPSK).
p = welch_pgram(waveform_vec, 1024, 0;
fs=gpswaveobj.SampleRate,
window = hann,
onesided=false)
The Welch method with the Hanna window provides a smoothed estimate by averaging the periodograms of individual segments.
Connecting auxiliary files and libraries
To visualize the results of the block, you need to connect third-party libraries.
let
installed_packages = collect(x.name for (_, x) in Pkg.dependencies() if x.is_direct_dep)
list_packages = ["DSP"]
for pack in list_packages
pack in installed_packages || Pkg.add(pack)
end
end
import DSP
using EngeeDSP, EngeeSatellites
# Creating and configuring a generator
gpswaveobj = gpsWaveformGenerator()
gpswaveobj.EnablePCode = true
gpswaveobj.SampleRate = 4 * 10.23e6
# Signal generation
navdata = rand([0,1], 2, 1)
waveform = gpswaveobj(navdata)
# Converting the signal matrix into a one-dimensional vector
waveform_vec = vec(waveform)
# nfft=1024 — the size of the FFT (the larger, the higher the resolution)
# We set the window length to n=1024 and the overlap to noverlap=0 (0%)
p = DSP.welch_pgram(
waveform_vec, 1024, 0;
fs=gpswaveobj.SampleRate,
window = DSP.hann,
onesided=false
)
# Extracting frequencies and spectral power density
freqs = DSP.freq(p) # frequency vector in Hz
psd = DSP.power(p) # PSD vector in W/Hz
# Centering the spectrum
freqs_shifted = EngeeDSP.Functions.fftshift(freqs)
psd_shifted = EngeeDSP.Functions.fftshift(psd)
# Plotting the spectrum in dBW/Hz
plot(freqs_shifted/1e6, 10*log10.(psd_shifted .+ eps()),
xlabel="Frequency (MHz)",
ylabel="Мощность (dBW/Гц)",
title="GPS P-code Spectrum",
legend=false)
The L1C signal
L1C — This signal is open for civilian use and is transmitted on the L1 frequency. It uses a binary carrier offset (BOC) modulation scheme
The global difference from the previous script is the presence of two satellites and, accordingly, two signals. It is based on the same basic principles as in P-code generation.
prn = [4, 70] # satellite IDs
numsat = length(prn) # 2 satellites
numbits = 100 # message length in bits
navdata = rand([0, 1], numbits, numsat)
fs = 25e6 # Sampling rate 25 MHz
gpswaveobj = gpsWaveformGenerator(; SignalType="l1c", PRNID=prn, SampleRate=fs)
waveform = gpswaveobj(navdata) # This is an Nx2 matrix, where N is the number of samples.
# Parameter setting for Welch (spectrum smoothing)
# If the signal is shorter than 2048 points, we use its full length.
seg_len = min(2048, size(waveform, 1))
# Plotting a graph
# Creating an empty area with axis labels and a legend
plot_obj = plot(; xlabel="Frequency (MHz)", ylabel="Мощность (dBW/Гц)",
title="GPS L1C Spectrum (PRN 4 and 70)", legend=:topleft)
# Cycle through two channels (satellites)
for i in 1:numsat
# We take only the ith column (channel)
channel = waveform[:, i]
# We calculate the spectrum using the Welch method
p_i = DSP.welch_pgram(channel, seg_len, 0; fs=fs, onesided=false)
# Extracting and centering the frequencies
freqs_i = DSP.freq(p_i)
psd_i = DSP.power(p_i)
freqs_shifted = EngeeDSP.Functions.fftshift(freqs_i)
psd_shifted = EngeeDSP.Functions.fftshift(psd_i)
# Adding a graph
plot!(plot_obj, freqs_shifted / 1e6, 10 * log10.(psd_shifted .+ eps()),
label="PRN $(prn[i])")
end
# Displaying a graph
display(plot_obj)
L2C signal
SignalType="l2c" – the basis is the civilian signal L2C. It is transmitted at a frequency of 1227.60 MHz and contains two time-multiplexed codes.:
-
CM (Civil Moderate) is a 10,230-chip code (20 ms period), modulated by CNAV data (25 bits/s).
-
CL (Civil Long) – code with a length of 767,250 chips (1.5 s period), without data (pilot component).
Both codes have a chip speed of 511.5 kchip/s, but are multiplexed in such a way that the total chip speed is 1,023 Kchip/s (as in C/A).
waveform = gpswaveobj(lnavdata, cnavdata)
- Unlike the previous L1C example, here data is transmitted ** as a tuple of two matrices**, because L2C uses two different navigation streams: LNAV and CNAV. The generator uses them to modulate the corresponding components.
The number of satellites has been increased to four, but otherwise the principle of operation remains the same.
# Setting the parameters
prn = [7, 11, 20, 28]
numsat = length(prn)
numbits = 10
# LNAV and CNAV random data generation (10x4 matrices)
lnavdata = rand([0, 1], numbits, numsat)
cnavdata = rand([0, 1], numbits, numsat)
# Creating a generator with settings
gpswaveobj = gpsWaveformGenerator(;
SignalType="l2c",
PRNID=prn,
SampleRate=15e6,
EnablePCode=true
)
# Signal generation
# attention: Transmitting data in the form of a tuple (analogous to a MATLAB cell array)
waveform = gpswaveobj(lnavdata, cnavdata)
# Checking the size
println("The size of the generated signal: ", size(waveform))
# Expected output: (3000000, 4)
# Adjusting the FFT for smoothing
seg_len = min(2048, size(waveform, 1))
# Plotting a graph
plot_obj = plot(; xlabel="Frequency (MHz)", ylabel="Мощность (dBW/Гц)",
title="GPS L2C Spectrum (PRN 7, 11, 20, 28)", legend=:topleft)
for i in 1:numsat
channel = waveform[:, i] # We take the channel of the i-th satellite
# Calculating the spectrum
p_i = DSP.welch_pgram(channel, seg_len, 0; fs=15e6, onesided=false, nfft=seg_len)
freqs_i = DSP.freq(p_i)
psd_i = DSP.power(p_i)
freqs_shifted = EngeeDSP.Functions.fftshift(freqs_i)
psd_shifted = EngeeDSP.Functions.fftshift(psd_i)
# Drawing a graph for this satellite
plot!(plot_obj, freqs_shifted / 1e6, 10 * log10.(psd_shifted .+ eps()),
label="PRN $(prn[i])")
end
display(plot_obj) # Displaying a graph
The L5 signal
The GPS L5 signal is one of the modern civilian GPS system signals transmitted at a frequency of 1176.45 MHz (L5 band). It was introduced as part of the GPS modernization program and is designed primarily to provide the high accuracy and integrity needed for aviation and other critical (Safety-of-Life) applications.
The main body of the script remains similar to the previous ones, however, distortions are added to the signal caused by the Doppler shift resulting from the movement of the satellite and receiver. The introduction of different shifts (+3 kHz and -4.6 kHz) spreads their spectra, which leads to the appearance of two close maxima inside the common envelope – an interesting and visual effect.
Distortion: Doppler frequency shift
# Внесение искажений
# Частотный сдвиг
t = (0:size(waveform, 1) - 1) / fs # Вектор времени
freq_offsets = [3e3, -4.6e3] # Сдвиги для двух каналов
# Создаем комплексную экспоненту и умножаем на сигнал
fwave = waveform .* exp.(im * 2 * pi * t .* freq_offsets')
# Внесение временной задержки
delay_val = 5.23e-4
delay_samples = round(Int, delay_val * fs) # 13075 отсчетов
# Физическая задержка: добавляем нули в начало и отбрасываем конец
dwave = vcat(zeros(ComplexF64, delay_samples, numsat), fwave[1:end-delay_samples, :])
# Суммирование двух каналов
bbwave = sum(dwave, dims=2) # Размер (10_000_000 x 1)
bbwave_vec = vec(bbwave) # Превращаем в одномерный вектор
# GPS L5 signal generation
prn = [185, 189]
numsat = length(prn)
numbits = 40
msg = rand([0, 1], numbits, numsat)
fs = 25e6
gpswaveobj = gpsWaveformGenerator(;
SignalType="l5",
PRNID=prn,
InitialTime=42123,
SampleRate=fs
)
# We are generating a signal. We get the matrix (10_000_000 x 2)
waveform = gpswaveobj(msg)
println("The size of the original signal: ", size(waveform))
# Introducing distortions
# Frequency shift
t = (0:size(waveform, 1) - 1) / fs # Time vector
freq_offsets = [3e3, -4.6e3] # Shifts for two channels
# We create a complex exponent and multiply it by the signal
fwave = waveform .* exp.(im * 2 * pi * t .* freq_offsets')
# Introducing a time delay
delay_val = 5.23e-4
delay_samples = round(Int, delay_val * fs) # 13075 counts
# Physical delay: add zeros to the beginning and discard the end
dwave = vcat(zeros(ComplexF64, delay_samples, numsat), fwave[1:end-delay_samples, :])
# Summing two channels
bbwave = sum(dwave, dims=2) # Size (10_000_000 x 1)
bbwave_vec = vec(bbwave) # We turn it into a one-dimensional vector
println("Size of the resulting signal (BB): ", size(bbwave_vec))
# Visualization of the spectrum
seg_len = min(2048, length(bbwave_vec))
# Calculating the spectrum
p = DSP.welch_pgram(bbwave_vec, seg_len, 0; fs=fs, onesided=false, nfft=seg_len)
freqs = DSP.freq(p)
psd = DSP.power(p)
# Centering the spectrum
freqs_shifted = EngeeDSP.Functions.fftshift(freqs)
psd_shifted = EngeeDSP.Functions.fftshift(psd)
# Drawing a graph
plot(freqs_shifted / 1e6, 10 * log10.(psd_shifted .+ eps()),
xlabel="Frequency (MHz)",
ylabel="Мощность (dBW/Гц)",
title="GPS L5 Spectrum",
legend=false)
Conclusion
The presented examples allow us to evaluate the operation of the gpsWaveformGenerator unit, its functionality and the effect of settings on the resulting GPS signal at the output.