question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

time_gate with multiplication in time domain

See original GitHub issue

For time gate skrf currently only supports convolution in frequency domain. In some cases it is more useful to multiply the gate in time domain.

The modification would be placed in time.py within the function time_gate and could be accessed by a new variable to switch between fd and td.

Code as optional path starting from line 300 … 312 in scikit-rf/skrf/time.py

out = ntwk.copy()
s_td = np.fft.ifft(out.s[:,0,0])
s_td_g = s_td * np.fft.ifftshift(gate)
s_fd_g = np.fft.fft(s_td_g)
out.s[:,0,0] = s_fd_g

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Vinc0110commented, Jul 22, 2022

We have a winner! As expected, the time-domain multiplication is much faster than the frequency-domain convolution. And both results are nearly identical, so I think the choice is a no-brainer.

In the results below, I think the gating only works for $N>1000$ samples, hence the jump in the rms errors:

window=(‘tukey’, 0.66): tukey_066

window=(‘kaiser’, 6): kaiser_6

2reactions
Vinc0110commented, Jul 21, 2022

Found two problems in the implementation of the frequency-domain gating:

  1. the convolution with mode='reflect' caused significant boundary effects. Using mode='wrap' works better

  2. the frequency transform of the window was normalized in a strange way. Not sure what was going on there, but I think I found a much simpler and straight forward solution.

With the code below, both approaches now yield similar (identical?) results. I’ll do some more testing and then create a PR to fix this issue during the weekend.

gating

from skrf.media.coaxial import Coaxial
import skrf as rf
import matplotlib.pyplot as plt
import numpy as np

from scipy.ndimage import convolve1d
from scipy import signal

# settings
freq = rf.Frequency(0, 20, 4001,'GHz')
window_type = ('tukey', 0.66)
#window_type = ('kaiser', 6)
t_center = (1+0.5)*2+0.1
t_span = 1

# create network
coax_media = Coaxial(frequency=freq, z0=50)
coax_line_1 = coax_media.line(1, 'ns', z0=65)
coax_line_2 = coax_media.line(0.5, 'ns', z0=50)
coax_line_3 = coax_media.line(0.1, 'ns', z0=55)
coax_line_4 = coax_media.line(0.5, 'ns', z0=50)
coax_line_5 = coax_media.line(1, 'ns', z0=65)
ntw_total = coax_line_1 ** coax_line_2 ** coax_line_3 ** coax_line_4 ** coax_line_5

# find start/stop gate indecies
t = ntw_total.frequency.t
t_start = t_center - 0.5 * t_span
t_stop = t_center + 0.5 * t_span
start_idx = (np.abs(t - t_start * 1e-9)).argmin()
stop_idx = (np.abs(t - t_stop * 1e-9)).argmin()

# create window
window_width = abs(stop_idx - start_idx)
window = signal.get_window(window_type, window_width)

# create the gate by padding the window with zeros
gate = np.zeros_like(t)
gate[start_idx:stop_idx] = window

# time-domain gating
ntw_gated_td = ntw_total.s11.copy()
s_td = np.fft.ifftshift(np.fft.ifft(ntw_gated_td.s[:, 0, 0]))
s_td_g = s_td * gate
ntw_gated_td.s[:,0,0] = np.fft.fft(np.fft.fftshift(s_td_g))

# frequency-domain gating
ntw_gated_fd = ntw_total.s11.copy()
kernel = np.fft.ifftshift(np.fft.fft(np.fft.fftshift(gate), norm='forward'))
ntw_gated_fd.s[:, 0, 0] = convolve1d(ntw_gated_fd.s[:, 0, 0], kernel, mode='wrap')

# plotting
fig, ax = plt.subplots(4, 1)

ntw_total.s11.plot_s_time_mag(label='input data', ax=ax[0])

ntw_gated_td.plot_s_time_mag(label='gated in TD', ax=ax[1])
ax[1].plot(ntw_total.frequency.t_ns, gate * 0.05, '--')

ntw_gated_fd.plot_s_time_mag(label='gated in FD', ax=ax[2])
ax[2].plot(ntw_total.frequency.t_ns, gate * 0.05, '--')

ntw_gated_td.plot_s_db(label='gated in TD', ax=ax[3])
ntw_gated_fd.plot_s_db(label='gated in FD', ax=ax[3])

ax[0].set_xlim(0, 5)
ax[1].set_xlim(0, 5)
ax[2].set_xlim(0, 5)

plt.tight_layout()
plt.show()

Read more comments on GitHub >

github_iconTop Results From Across the Web

skrf.time.time_gate — scikit-rf Documentation - Read the Docs
With 'fft' (default), the data gets transformed into time-domain using inverse FFT and the gating is achieved by multiplication with the time-domain gate....
Read more >
(PDF) Gating effects in time domain transforms - ResearchGate
Time Gate shape for various gate center time choices ... Gating can be thought of as multiplying the time domain response by a...
Read more >
Advanced Antenna Measurement Techniques using Time ...
Time domain data is obtained mathematically from frequency domain. Vector antenna responses in frequency domain can be transformed to time domain.
Read more >
scikit-rf/time.py at master - GitHub
by multiplication with the time-domain gate. The gated time-domain signal is then transformed back into. frequency-domain using inverse FFT.
Read more >
Multiplying Signals - University of California, Berkeley
We have seen that convolution in the time domain corresponds to multiplication in the frequency domain. It turns out that this relationship is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found