• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MHKiT-Software / MHKiT-Python / 11367210922

16 Oct 2024 01:57PM UTC coverage: 85.658%. First build
11367210922

Pull #349

github

web-flow
Merge 4f0c18b1d into 6445365d0
Pull Request #349: Acoustics Module

344 of 472 new or added lines in 5 files covered. (72.88%)

9341 of 10905 relevant lines covered (85.66%)

5.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

18.52
/mhkit/acoustics/graphics.py
1
"""
3✔
2
This submodule includes the main passive acoustics plotting functions. All
3
functions allow passthrough of matplotlib functionality and commands
4
to make them fully customizable.
5
"""
6

7
import matplotlib.pyplot as plt
6✔
8
from .analysis import _fmax_warning
6✔
9

10

11
def plot_spectogram(spsdl, fmin=10, fmax=100000, fig=None, ax=None, **kwargs):
6✔
12
    """
13
    Plots the spectogram of the sound pressure spectral density level.
14

15
    Parameters
16
    ----------
17
    spsdl: xarray DataArray (time, freq)
18
        Mean square sound pressure spectral density level in dB rel 1 uPa^2/Hz
19
    fmin: int
20
        Lower frequency band limit (lower limit of the hydrophone). Default: 10 Hz
21
    fmax: int
22
        Upper frequency band limit (Nyquist frequency). Default: 100000 Hz
23
    fig: matplotlib.pyplot.figure
24
        Figure handle to plot on
25
    ax: matplotlib.pyplot.axis
26
        Figure axis containing plot objects
27
    kwargs: dict
28
        Dictionary of matplotlib function keyword arguments
29

30
    Returns
31
    -------
32
    fig: matplotlib.pyplot.figure
33
        Figure handle of plot
34
    ax: matplotlib.pyplot.axis
35
        Figure plot axis
36
    """
37

38
    # Set dimension names
NEW
39
    time = spsdl.dims[0]
×
NEW
40
    freq = spsdl.dims[-1]
×
41
    # Check fmax
NEW
42
    fn = spsdl[freq].max().item()
×
NEW
43
    fmax = _fmax_warning(fn, fmax)
×
44
    # select frequency range
NEW
45
    spsdl = spsdl.sel({freq: slice(fmin, fmax)})
×
46

NEW
47
    if ax is None:
×
NEW
48
        fig, ax = plt.subplots(figsize=(6, 5), subplot_kw={"yscale": "log"})
×
NEW
49
        fig.subplots_adjust(left=0.1, right=0.95, top=0.97, bottom=0.11)
×
NEW
50
    h = ax.pcolormesh(
×
51
        spsdl[time].values, spsdl[freq].values, spsdl.T, shading="nearest", **kwargs
52
    )
NEW
53
    fig.colorbar(h, ax=ax, label=spsdl.units)
×
NEW
54
    ax.set(xlabel="Time", ylabel="Frequency [Hz]")
×
55

NEW
56
    return fig, ax
×
57

58

59
def plot_spectra(spsdl, fmin=10, fmax=100000, fig=None, ax=None, **kwargs):
6✔
60
    """
61
    Plots spectral density. X axis is log-transformed.
62

63
    Parameters
64
    ----------
65
    spsdl: xarray DataArray (time, freq)
66
        Mean square sound pressure spectral density level in dB rel 1 uPa^2/Hz
67
    fmin: int
68
        Lower frequency band limit (lower limit of the hydrophone). Default: 10 Hz
69
    fmax: int
70
        Upper frequency band limit (Nyquist frequency). Default: 100000 Hz
71
    fig: matplotlib.pyplot.figure
72
        Figure handle to plot on
73
    ax: matplotlib.pyplot.axis
74
        Figure axis containing plot objects
75
    kwargs: dict
76
        Dictionary of matplotlib function keyword arguments
77

78
    Returns
79
    -------
80
    fig: matplotlib.pyplot.figure
81
        Figure handle of plot
82
    ax: matplotlib.pyplot.axis
83
        Figure plot axis
84
    """
85

86
    # Set dimension names
NEW
87
    freq = spsdl.dims[-1]
×
88
    # Check fmax
NEW
89
    fn = spsdl[freq].max().item()
×
NEW
90
    fmax = _fmax_warning(fn, fmax)
×
91
    # select frequency range
NEW
92
    spsdl = spsdl.sel({freq: slice(fmin, fmax)})
×
93

NEW
94
    if ax is None:
×
NEW
95
        fig, ax = plt.subplots(figsize=(6, 5), subplot_kw={"xscale": "log"})
×
NEW
96
        fig.subplots_adjust(
×
97
            left=0.1, right=0.95, top=0.85, bottom=0.2, hspace=0.3, wspace=0.15
98
        )
NEW
99
    ax.plot(spsdl[freq], spsdl.T, **kwargs)
×
NEW
100
    ax.set(xlim=(fmin, fmax), xlabel="Frequency [Hz]", ylabel=spsdl.units)
×
101

NEW
102
    return fig, ax
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc