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

Signal #6

Merged
merged 10 commits into from
Jun 12, 2024
Merged

Signal #6

merged 10 commits into from
Jun 12, 2024

Commits on May 25, 2024

  1. Add a signal module (starting with an implementation of the Kaiser fu…

    …nction)
    
    This module will have signal processing related functions (e.g. to generate windows, resampling, etc).
    
    The first set of functions added to this module allow you to calculate the Kaiser window.
    To do so, we also add an implementation of the Bessel function of the first kind of order 0 (i0).
    
    The code in this commit is based on the code for the corresponding functions in the scipy library.
    AngelEzquerra committed May 25, 2024
    Configuration menu
    Copy the full SHA
    0285282 View commit details
    Browse the repository at this point in the history
  2. Add firls function (least-squares minimization based FIR filter des…

    …ign)
    
    Adds a new `firls` procedure that can be used to design a linear phase FIR filter using least-squares error criterion. This function is avaiable in `scpy` (as `scipy.signal.firls`). This version is similar but has some differences in its interface and also as some additional functionality (it can design a broader set of filter types).
    
    `firls` eturns the coefficients of the linear phase FIR filter of the requested
    order and type which best fullfills the desired frequency response
    "constraints" described by the `bands` (i.e. frequency), `desired` (i.e. gain)
    and `weights` (e.i. constraint weights) input tensors.
    
    Depending on the order and the value of the `symmetric` argument, the
    output filter coefficients will correspond to a Type I, II, III or IV
    FIR filter.
    
    The constraints are specified as a pair of tensors describing "constrained
    frequency bands" indicating the start and end frequencies of each band `k`,
    as well as the gain of the filter at those edge frequencies.
    
    Inputs:
      - fir_order: The "order" of the filter (which is 1 less than the length
                   of the output tensor).
      - bands: 2-column matrix of non-overlapping, normalized frequency band
               edges in ascending order. Each row in the matrix corresponds to
               the edges of a constrained frequency band (the column contains
               the start frequency of the band and the second column contains
               the end frequency).
               Frequencies between the specified bands are "unconstrained"
               (i.e. ignored during the error minimization process).
               Frequency values must be in the [0.0, fs/2] range (i.e. they
               cannot be negative or exceed the Nyquist frequency).
      - desired: 2-column matrix that specifies the gains of the desired
                 frequency response at the band "edges". Thus the lengths of
                 the `bands` and `desired` tensors must match. If they do not,
                 an exception is raised.
                 For each band `k` the desired filter frequency
                 response is such that its gain linearly changes from the
                 `desired[k, 0]` value at the start of the band (i.e. at the
                 `bands[k, 0]` frequency) to the value `desired[k, 1]` at the
                 end of the band (i.e. at `bands[k, 1]`).
      - weights: Optional rank-1 Tensor of weights. Controls which frequency
                 response "contraints" are given more "weight" during the
                 least-squares error minimization process. The default is that
                 all constraints are given the same weight. If provided, its
                 length must be half the length of `bands` (i.e. there must be
                 one constraint per band). An exception is raised otherwise.
      - symmetric: When `true` (the default), the result will be a symmetric
                   FIR filter (Type I when `fir_order` is even and Type II
                   when `fir_order` is odd). When `false`, the result will be
                   an anti-symmetric FIR filter (Type III when `fir_order` is
                   even and Type IV when `fir_order` is odd).
      - fs: The sampling frequency of the signal (as a float). Each frequency
            in `bands` must be between 0.0 and `fs/2` (inclusive).
            Default is 2.0, which means that by default the band frequencies
            are expected to be on the 0.0 to 1.0 range.
    
    Result:
      - A Tensor containing the `fir_order + 1` taps of the FIR filter that
        best approximates the desired frequency response (i.e. the filter that
        minimizes the least-squares error vs the given constraints).
    
    Notes:
      - Contrary to numpy's firls, the first argument is the FIR filter order,
        not the FIR length. The filter length is `filter_order + 1`.
      - Frequencies between "constrained bands" are considered "don't care"
        regions for which the error is not minimized.
      - When designing a filter with a gain other than zero at the Nyquist
        frequency (i.e. when `bands[^1] != 0.0`), such as high-pass and
        band-stop filters, the filter order must be even. An exception is
        raised otherwise.
      - The `bands` and `desired` can also be flat rank-1 tensors, as long as
        their length is even (so that they can be reshaped into 2 column
        matrices.
    
    Examples:
    ```nim
    echo firls(4, [[0.0, 0.3], [0.4, 1.0]].toTensor, [[1.0, 1.0], [0.0, 0.0]].toTensor)
    
    echo firls(4, [0.0, 0.3, 0.4, 1.0].toTensor, [1.0, 1.0, 0.0, 0.0].toTensor)
    
    echo firls(4,
      [[0.0, 1.5], [2.0, 5.0]].toTensor,
      [[1.0, 1.0], [0.0, 0.0]].toTensor,
      fs = 10.0)
    
    echo firls(4,
      [[0.0, 0.3], [0.4, 1.0]].toTensor,
      [[1.0, 1.0], [0.0, 0.0]].toTensor,
      weights = [1.0, 0.5].toTensor)
    
    echo firls(5,
      [[0.0, 0.3], [0.3, 0.6], [0.6, 1.0]].toTensor,
      [[1.0, 1.0], [1.0, 0.2], [0.0, 0.0]].toTensor)
    
    echo firls(6,
      [[0.0, 0.4], [0.6, 1.0]].toTensor, [[0.0, 0.0], [0.9, 1.0]].toTensor,
      symmetric = false)
    
    Trying to design a high-pass filter with odd order generates an exception:
    echo firls(5,
      [0.0, 0.5, 0.6, 1.0].toTensor, [0.0, 0.0, 1.0, 1.0].toTensor,
      symmetric = false)
    ```
    AngelEzquerra committed May 25, 2024
    Configuration menu
    Copy the full SHA
    dd4809d View commit details
    Browse the repository at this point in the history
  3. Add an upfirdn procedure

    This procedure, which is available both in Matlab and scipy (as `scipy.signal.upfirdn`)  upsamples a rank-1 tensor, applies a FIR filter and downsamples it. It is basically a shortcut for a combination of upsample and convolve with a downsample factor. However this is a core signal processing operation that it deserves its own function.
    AngelEzquerra committed May 25, 2024
    Configuration menu
    Copy the full SHA
    769b4d1 View commit details
    Browse the repository at this point in the history

Commits on May 26, 2024

  1. Update arraymancer dependency version

    Also update the description a little.
    AngelEzquerra committed May 26, 2024
    Configuration menu
    Copy the full SHA
    febc2d1 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    46cde4c View commit details
    Browse the repository at this point in the history

Commits on May 27, 2024

  1. Fix README.md

    AngelEzquerra committed May 27, 2024
    Configuration menu
    Copy the full SHA
    0a7a98b View commit details
    Browse the repository at this point in the history

Commits on Jun 9, 2024

  1. Add support for resampling tensors

    This adds a couple of `resample` procedures as well as some useful, supporting functions.
    These procedurs let you resample a tensor by a certain up / down sampling ratio.
    One version of the `resample` procedure lets you provide a specific antialiasing filter that will be used between the upsampling and downsampling operations. Another version automatically calculates an appropriate filter (using `firls`), given a specific "filter order factor" and a beta value for the kaiser window that is applied to the designed filter.
    AngelEzquerra committed Jun 9, 2024
    Configuration menu
    Copy the full SHA
    826f14f View commit details
    Browse the repository at this point in the history

Commits on Jun 11, 2024

  1. Configuration menu
    Copy the full SHA
    9ad92a9 View commit details
    Browse the repository at this point in the history

Commits on Jun 12, 2024

  1. Configuration menu
    Copy the full SHA
    d9b9cd1 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    478a812 View commit details
    Browse the repository at this point in the history