Created
October 22, 2024 14:21
-
-
Save pushkarsingh019/35dfeec160bd39cb6dbd63eb1814e88c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| # Parameters | |
| fs = 44100 # Sampling frequency | |
| starttime_fakepulse = 0.0 | |
| peaktime_fakepulse = starttime_fakepulse + 0.006 | |
| endtime_fakepulse = peaktime_fakepulse + 0.006 | |
| peakamp_fakepulse = 1.0 | |
| endamp_fakepulse = 0.0 | |
| ffake = 4000 # Frequency in Hz | |
| starttime_fake = 0 | |
| peaktime_fake = starttime_fake + 0.22 | |
| pulse_interval_fake = 0.300 / 7 | |
| pulsect = 11 | |
| endtime_fake = starttime_fake + pulsect * pulse_interval_fake | |
| peakamp_fake = 0.028 | |
| # Generate time points | |
| starttime_fake_pulsesamp = int(np.ceil(starttime_fakepulse * fs)) | |
| peaktime_fake_pulsesamp = int(np.round(peaktime_fakepulse * fs)) | |
| endtime_fake_pulsesamp = int(np.floor(endtime_fakepulse * fs)) | |
| starttime_fake_samp = int(np.ceil(starttime_fake * fs)) | |
| peaktime_fake_samp = int(np.round(peaktime_fake * fs)) | |
| endtime_fake_samp = int(np.floor(endtime_fake * fs)) | |
| pulse_interval_fake_samp = int(np.round(pulse_interval_fake * fs)) | |
| # Create the envelope for the fake pulse | |
| envelope_fakepulse = np.zeros(endtime_fake_pulsesamp) | |
| envelope_fakepulse[starttime_fake_pulsesamp:peaktime_fake_pulsesamp] = peakamp_fakepulse * np.sin( | |
| 0.5 * np.pi * np.linspace(0, 1, peaktime_fake_pulsesamp - starttime_fake_pulsesamp) | |
| ) # Removed +1 to match slice size | |
| envelope_fakepulse[peaktime_fake_pulsesamp:endtime_fake_pulsesamp] = peakamp_fakepulse * np.cos( | |
| 0.5 * np.pi * np.linspace(0, 1, endtime_fake_pulsesamp - peaktime_fake_pulsesamp) | |
| ) | |
| # Create the phase for the fake pulse (sinusoidal carrier wave) | |
| t = np.arange(0, endtime_fake_pulsesamp - starttime_fake_pulsesamp) | |
| phase_frogfakepulse = np.sin(2 * np.pi * ffake * t / fs) | |
| # Combine envelope and carrier to create the pulse | |
| frogfakepulse = envelope_fakepulse * phase_frogfakepulse | |
| # Create the envelope for the whole frog call | |
| envelope_fake = np.zeros(endtime_fake_samp) | |
| envelope_fake[starttime_fake_samp:peaktime_fake_samp] = peakamp_fake * np.sin( | |
| 0.5 * np.pi * np.linspace(0, 1, peaktime_fake_samp - starttime_fake_samp) | |
| ) | |
| tcfake = -(endtime_fake_samp - peaktime_fake_samp) / np.log(endamp_fakepulse / (1.001 * peakamp_fake)) | |
| envelope_fake[peaktime_fake_samp:endtime_fake_samp] = peakamp_fake * np.exp( | |
| -np.arange(1, endtime_fake_samp - peaktime_fake_samp + 1) / tcfake | |
| ) | |
| # Assemble the full frog call by repeating pulses | |
| frogfake = np.zeros_like(envelope_fake) | |
| for ii in range(pulsect): | |
| start_idx = starttime_fake_samp + ii * pulse_interval_fake_samp | |
| end_idx = start_idx + endtime_fake_pulsesamp | |
| frogfake[start_idx:end_idx] = frogfakepulse | |
| # Multiply envelope by repeated pulses | |
| frogfake = envelope_fake * frogfake | |
| # Plot the results | |
| plt.figure(figsize=(10, 4)) | |
| plt.plot(frogfake) | |
| plt.title('Synthetic Gray Tree Frog Call') | |
| plt.xlabel('Time (samples)') | |
| plt.ylabel('Amplitude') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment