Skip to content
Snippets Groups Projects
Commit 61be4841 authored by Olle Hansson's avatar Olle Hansson
Browse files

Added doc and test for signalgenerator FromFile

parent b11aa808
No related branches found
No related tags found
No related merge requests found
Pipeline #90331 failed
......@@ -103,6 +103,11 @@ class ZeroPadInput(SignalGeneratorInput):
class FromFileInput(SignalGeneratorInput):
"""
Class for graphically configuring and generating a
:class:`~b_asic.signal_generators.FromFile` signal generator.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.file_label = QLabel("Browse")
......
......@@ -13,6 +13,7 @@ if you want more information.
from math import pi, sin
from numbers import Number
from pathlib import Path
from typing import Optional, Sequence
import numpy as np
......@@ -172,12 +173,17 @@ class FromFile(SignalGenerator):
"""
def __init__(self, path) -> None:
try:
Path(path).resolve(strict=True)
except FileNotFoundError:
raise Exception("Selected input file not found.")
try:
data = np.loadtxt(path, dtype=complex).tolist()
self._data = data
self._len = len(data)
except FileNotFoundError:
self._window.logger.error(f"Selected input file not found.")
except ValueError:
raise Exception("Selected input file is not of the right format.")
def __call__(self, time: int) -> complex:
if 0 <= time < self._len:
......@@ -185,7 +191,7 @@ class FromFile(SignalGenerator):
return 0.0
def __repr__(self) -> str:
return f"ZeroPad({self._data})"
return f"FromFile({self._data})"
class Sinusoid(SignalGenerator):
......
import os
from math import sqrt
import pytest
......@@ -5,6 +6,7 @@ import pytest
from b_asic.signal_generator import (
Constant,
Delay,
FromFile,
Gaussian,
Impulse,
Sinusoid,
......@@ -269,3 +271,14 @@ def test_division():
assert str(g) == "Sinusoid(0.5, 0.25) / (0.5 * Step())"
assert isinstance(g, _DivGenerator)
def test_fromfile():
absolute_path = os.path.dirname(__file__)
relative_path = "../examples/input.csv"
full_path = os.path.join(absolute_path, relative_path)
g = FromFile(full_path)
assert g(-1) == 0.0
assert g(0) == 0
assert g(1) == 1
assert g(2) == 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment