Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format and fix unit conversion error #28

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Configuration file for JuliaFormatter.jl
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/

margin = 92
remove_extra_newlines = true
separate_kwargs_with_semicolon = true
format_docstrings = true
format_markdown = true
6 changes: 3 additions & 3 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
The package GNSSSignals is licensed under the MIT License.

> Copyright (c) 2017-2019: Soeren Zorn
>
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
> "Software"), to deal in the Software without restriction, including
> without limitation the rights to use, copy, modify, merge, publish,
> distribute, sublicense, and/or sell copies of the Software, and to
> permit persons to whom the Software is furnished to do so, subject to
> the following conditions:
>
>
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
>
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GNSSSignals"
uuid = "52c80523-2a4e-5c38-8979-05588f836870"
authors = ["Soeren Schoenbrod <[email protected]>", "Michael Niestroj <[email protected]"]
version = "0.17.0"
version = "0.17.1"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand Down
115 changes: 56 additions & 59 deletions src/GNSSSignals.jl
Original file line number Diff line number Diff line change
@@ -1,64 +1,61 @@
module GNSSSignals

using Core: toInt16
using DocStringExtensions
using FixedPointNumbers
using Statistics
using Unitful: Frequency, Hz

import Base.show

export
AbstractGNSS,
GPSL1,
GPSL5,
GalileoE1B,
gen_code!,
gen_code,
get_codes,
get_code_type,
get_code_length,
get_secondary_code_length,
get_center_frequency,
get_code_frequency,
get_code,
get_code_unsafe,
get_data_frequency,
get_code_center_frequency_ratio,
get_subcarrier_frequency,
get_code_spectrum,
get_system_string,
min_bits_for_code_length,
get_modulation,
get_secondary_code


abstract type AbstractGNSS{C} end

Base.Broadcast.broadcastable(system::AbstractGNSS) = Ref(system)

Base.show(io::IO, x::AbstractGNSS) = print("$(typeof(x))()")

"""
$(SIGNATURES)

Reads Int8 encoded codes from a file with filename `filename` (including the path). The
code length must be provided by `code_length` and the number of PRNs by `num_prns`.
# Examples
```julia-repl
julia> read_in_codes("/data/gpsl1codes.bin", 32, 1023)
```
"""
function read_in_codes(type, filename, num_prns, code_length)
open(filename) do file_stream
read!(file_stream, Array{type}(undef, code_length, num_prns))
end
using Core: toInt16
using DocStringExtensions
using FixedPointNumbers
using Statistics
using Unitful: Frequency, Hz, upreferred

import Base.show

export AbstractGNSS,
GPSL1,
GPSL5,
GalileoE1B,
gen_code!,
gen_code,
get_codes,
get_code_type,
get_code_length,
get_secondary_code_length,
get_center_frequency,
get_code_frequency,
get_code,
get_code_unsafe,
get_data_frequency,
get_code_center_frequency_ratio,
get_subcarrier_frequency,
get_code_spectrum,
get_system_string,
min_bits_for_code_length,
get_modulation,
get_secondary_code

abstract type AbstractGNSS{C} end

Base.Broadcast.broadcastable(system::AbstractGNSS) = Ref(system)

Base.show(io::IO, x::AbstractGNSS) = print("$(typeof(x))()")

"""
$(SIGNATURES)

Reads Int8 encoded codes from a file with filename `filename` (including the path). The
code length must be provided by `code_length` and the number of PRNs by `num_prns`.
# Examples
```julia-repl
julia> read_in_codes("/data/gpsl1codes.bin", 32, 1023)
```
"""
function read_in_codes(type, filename, num_prns, code_length)
open(filename) do file_stream
read!(file_stream, Array{type}(undef, code_length, num_prns))
end
end


include("modulation.jl")
include("gps_l1.jl")
include("gps_l5.jl")
include("galileo_e1b.jl")
include("common.jl")
include("modulation.jl")
include("gps_l1.jl")
include("gps_l5.jl")
include("galileo_e1b.jl")
include("common.jl")
end
59 changes: 27 additions & 32 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ function gen_code!(
sampling_frequency::Frequency,
code_frequency::Frequency = get_code_frequency(gnss),
start_phase = 0.0,
start_index::Integer = 0
start_index::Integer = 0,
)
code_frequency > sampling_frequency && error("The code freqeuncy must not be larger than the sampling frequency.")
code_frequency > sampling_frequency &&
error("The code freqeuncy must not be larger than the sampling frequency.")
num_samples = length(code)
fixed_point = sizeof(Int) * 8 - 1 - min_bits_for_code_length(gnss)
FP = Fixed{Int, fixed_point}
FP = Fixed{Int,fixed_point}
total_code_length = get_code_length(gnss) * get_secondary_code_length(gnss)
fp_total_code_length = FP(total_code_length / get_code_factor(gnss))
delta = FP(code_frequency / sampling_frequency)
code_phase = FP(mod(FP(mod(start_phase, total_code_length)) + start_index * delta, total_code_length))
delta = FP(upreferred(code_frequency / sampling_frequency))
code_phase = FP(
mod(
FP(mod(start_phase, total_code_length)) + start_index * delta,
total_code_length,
),
)
@inbounds for i ∈ 1:num_samples
code[i] = get_code_unsafe(gnss, code_phase, prn)
code_phase += delta
Expand All @@ -59,18 +65,10 @@ function gen_code(
sampling_frequency::Frequency,
code_frequency::Frequency = get_code_frequency(gnss),
start_phase = 0.0,
start_index::Integer = 0
start_index::Integer = 0,
)
code = zeros(get_code_type(gnss), num_samples)
gen_code!(
code,
gnss,
prn,
sampling_frequency,
code_frequency,
start_phase,
start_index
)
gen_code!(code, gnss, prn, sampling_frequency, code_frequency, start_phase, start_index)
end

"""
Expand All @@ -91,7 +89,7 @@ $(SIGNATURES)
Get the minimum number of bits that are needed to represent the code length
"""
@inline function min_bits_for_code_length(gnss::AbstractGNSS)
ndigits(get_code_length(gnss) * get_secondary_code_length(gnss); base=2)
ndigits(get_code_length(gnss) * get_secondary_code_length(gnss); base = 2)
end

"""
Expand Down Expand Up @@ -127,63 +125,60 @@ $(SIGNATURES)
Get secondary code at phase
"""
@inline function get_secondary_code(gnss::AbstractGNSS, code::Tuple, phase)
code[mod(floor(Int, phase / get_code_length(gnss)), get_secondary_code_length(gnss)) + 1]
code[mod(floor(Int, phase / get_code_length(gnss)), get_secondary_code_length(gnss))+1]
end


"""
$(SIGNATURES)

Calculate the spectral power of a BPSK modulated signal with chiprate `fc`
at baseband frequency `f`
"""
function get_code_spectrum_BPSK(fc::Frequency, f)
return get_code_spectrum_BPSK(fc/1Hz, f)
return get_code_spectrum_BPSK(fc / 1Hz, f)
end
function get_code_spectrum_BPSK(fc, f::Frequency)
return get_code_spectrum_BPSK(fc, f/1Hz)
return get_code_spectrum_BPSK(fc, f / 1Hz)
end
function get_code_spectrum_BPSK(fc::Frequency, f::Frequency)
return get_code_spectrum_BPSK(fc/1Hz, f/1Hz)
return get_code_spectrum_BPSK(fc / 1Hz, f / 1Hz)
end
function get_code_spectrum_BPSK(fc, f)
return sinc(f/fc)^2 / fc
return sinc(f / fc)^2 / fc
end


"""
$(SIGNATURES)
Calculate the spectral power of a sine phased BOC modulated signal with chiprate
`fc` and subcarrier frequency `fs` at baseband frequency `f`
"""
function get_code_spectrum_BOCsin(fc::Frequency, fs::Frequency, f)
return get_code_spectrum_BOCsin(fc/1Hz, fs/1Hz, f)
return get_code_spectrum_BOCsin(fc / 1Hz, fs / 1Hz, f)
end
function get_code_spectrum_BOCsin(fc, fs, f::Frequency)
return get_code_spectrum_BOCsin(fc, fs, f/1Hz)
return get_code_spectrum_BOCsin(fc, fs, f / 1Hz)
end
function get_code_spectrum_BOCsin(fc::Frequency, fs::Frequency, f::Frequency)
return get_code_spectrum_BOCsin(fc/1Hz, fs/1Hz, f/1Hz)
return get_code_spectrum_BOCsin(fc / 1Hz, fs / 1Hz, f / 1Hz)
end
function get_code_spectrum_BOCsin(fc, fs, f)
return ((sinc(f/fc) * tan(pi*f/(2*fs)))^2/ fc)
return ((sinc(f / fc) * tan(pi * f / (2 * fs)))^2 / fc)
end


"""
$(SIGNATURES)
Calculate the spectral power of a cosine phased BOC modulated signal with chiprate
`fc` and subcarrier frequency `fs` at baseband frequency `f`
"""
function get_code_spectrum_BOCcos(fc::Frequency, fs::Frequency, f)
return get_code_spectrum_BOCcos(fc/1Hz, fs/1Hz, f)
return get_code_spectrum_BOCcos(fc / 1Hz, fs / 1Hz, f)
end
function get_code_spectrum_BOCcos(fc, fs, f::Frequency)
return get_code_spectrum_BOCcos(fc, fs, f/1Hz)
return get_code_spectrum_BOCcos(fc, fs, f / 1Hz)
end
function get_code_spectrum_BOCcos(fc::Frequency, fs::Frequency, f::Frequency)
return get_code_spectrum_BOCcos(fc/1Hz, fs/1Hz, f/1Hz)
return get_code_spectrum_BOCcos(fc / 1Hz, fs / 1Hz, f / 1Hz)
end
function get_code_spectrum_BOCcos(fc, fs, f)
return (2 * sinc(f/fc) * sinpi(f/4fs)^2 / cospi(f/2fs))^2 / fc
return (2 * sinc(f / fc) * sinpi(f / 4fs)^2 / cospi(f / 2fs))^2 / fc
end
8 changes: 4 additions & 4 deletions src/galileo_e1b.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
GalileoE1B
"""
struct GalileoE1B{C <: AbstractMatrix} <: AbstractGNSS{C}
struct GalileoE1B{C<:AbstractMatrix} <: AbstractGNSS{C}
codes::C
end

Expand All @@ -11,17 +11,17 @@ get_system_string(s::GalileoE1B) = "GalileoE1B"

function read_from_documentation(raw_code)
raw_code_without_spaces = replace(replace(raw_code, " " => ""), "\n" => "")
code_hex_array = map(x -> parse(UInt16, x, base = 16), collect(raw_code_without_spaces))
code_hex_array = map(x -> parse(UInt16, x; base = 16), collect(raw_code_without_spaces))
code_bit_string = string(string.(code_hex_array, base = 2, pad = 4)...)
map(x -> parse(Int8, x, base = 2), collect(code_bit_string)) .* Int8(2) .- Int8(1)
map(x -> parse(Int8, x; base = 2), collect(code_bit_string)) .* Int8(2) .- Int8(1)
end

function read_galileo_e1b_codes()
read_in_codes(
Int8,
joinpath(dirname(pathof(GNSSSignals)), "..", "data", "codes_galileo_e1b.bin"),
50,
4092
4092,
)
end

Expand Down
4 changes: 2 additions & 2 deletions src/gps_l1.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct GPSL1{C <: AbstractMatrix} <: AbstractGNSS{C}
struct GPSL1{C<:AbstractMatrix} <: AbstractGNSS{C}
codes::C
end

Expand All @@ -11,7 +11,7 @@ function read_gpsl1_codes()
Int8,
joinpath(dirname(pathof(GNSSSignals)), "..", "data", "codes_gps_l1.bin"),
37,
1023
1023,
)
end

Expand Down
Loading
Loading