Untitled

 avatar
unknown
plain_text
a year ago
3.9 kB
4
Indexable
import numpy as np

class task_1_2:
    def __init__(self, fs=1000):
        """
        Initialize the class with a specific sampling rate.

        Args:
            fs (int): Sampling rate in Hz. Defaults to 1000Hz.
        """
        self.fs = fs
    
    def generate_linear_chirp(self, amplitude, period, duration, start_freq, end_freq, init_phase):
        """
        Generate a linear chirp signal.

        Args:
            amplitude (float): Amplitude of the generated signal.
            period (float): Period of the generated signal (s).
            duration (float): Duration of the generated signal (s).
            start_freq (float): Start frequency of the generated signal (Hz).
            end_freq (float): End frequency of the generated signal (Hz).
            init_phase (float): Initial phase of the generated signal (radius).

        Returns:
            t   numpy.array: Array of timestamps in seconds. Data type must be float.
            f_t numpy.array: Array of generated frequency values. Data type must be float.
            s_t numpy.array: Array of generated signal values. Daíta type must be float.
        
        >>> gen = task_1_2(1000)
        >>> t, f_t, s_t = gen.generate_linear_chirp(amplitude=1, period=1, duration=2, start_freq=1, end_freq=10, init_phase=0)
        >>> np.round(t[10], 5)
        0.01
        >>> np.round(f_t[10], 5)
        1.09
        >>> np.round(s_t[10], 5)
        0.99785
        
        """
        t = None
        f_t = None
        s_t = None

        # >>>>>>>>>>>>>>> YOUR CODE HERE <<<<<<<<<<<<<<<
        t = np.arange(0, duration, 1/self.fs)
        beta = (end_freq - start_freq) / period
        f_t = start_freq + beta * (t % period) 
        s_t = amplitude * np.cos(2 * np.pi * (start_freq * (t % period) + 0.5 * beta * (t % period) ** 2) + init_phase)
        # >>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<
        
        
        t = np.array(t).astype(float)
        f_t = np.array(f_t).astype(float)
        s_t = np.array(s_t).astype(float)
        return t, f_t, s_t
    
    def generate_quar_chirp(self, amplitude, period, duration, start_freq, end_freq, init_phase):
        """
        Generate a quadratic chirp signal.

        Args:
            amplitude (float): Amplitude of the generated signal.
            period (float): Period of the generated signal (s).
            duration (float): Duration of the generated signal (s).
            start_freq (float): Start frequency of the generated signal (Hz).
            end_freq (float): End frequency of the generated signal (Hz).
            init_phase (float): Initial phase of the generated signal (radius).

        Returns:
            t   numpy.array: Array of timestamps in seconds. Data type must be float.
            f_t numpy.array: Array of generated frequency values. Data type must be float.
            s_t numpy.array: Array of generated signal values. Daíta type must be float.
        
        >>> gen = task_1_2(1000)
        >>> t, f_t, s_t = gen.generate_quar_chirp(amplitude=1.0, period=3, duration=10, start_freq=1, end_freq=10, init_phase=0)
        >>> np.round(t[10], 5)
        0.01
        >>> np.round(f_t[10], 5)
        1.0001
        >>> np.round(s_t[10], 5)
        0.99803
        
        """
        t = None
        f_t = None
        s_t = None
        
        # >>>>>>>>>>>>>>> YOUR CODE HERE <<<<<<<<<<<<<<<
        t = np.arange(0, duration, 1/self.fs)
        beta = (end_freq - start_freq) / (period ** 2)
        f_t = start_freq + beta * (t % period) ** 2 
        s_t = amplitude * np.cos(2 * np.pi * (start_freq * (t % period) + beta * (t % period) ** 3 / 3) + init_phase)
        # >>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<
        
        t = np.array(t).astype(float)
        f_t = np.array(f_t).astype(float)
        s_t = np.array(s_t).astype(float)
        return t, f_t, s_t
Editor is loading...
Leave a Comment