Skip to content

Commit

Permalink
Fix filter dimensions and update b-modes
Browse files Browse the repository at this point in the history
  • Loading branch information
waltsims committed Dec 19, 2023
1 parent 8fa9098 commit 78f4ec1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
13 changes: 8 additions & 5 deletions examples/bmode_reconstruction_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from kwave.utils.dotdictionary import dotdict
from kwave.utils.signals import tone_burst, get_win
from kwave.utils.filters import gaussian_filter
from kwave.reconstruction.tools import log_compression
from kwave.reconstruction.tools import log_compression, db
from kwave.reconstruction.beamform import envelope_detection


Expand Down Expand Up @@ -197,8 +197,8 @@ def main():
# Frequency Filtering
# -----------------------------

scan_lines_fund = gaussian_filter(scan_lines.T, 1/kgrid.dt, tone_burst_freq, 100)
scan_lines_harm = gaussian_filter(scan_lines.T, 1/kgrid.dt, 2 * tone_burst_freq, 30) # plotting was not impl.
scan_lines_fund = gaussian_filter(scan_lines, 1/kgrid.dt, tone_burst_freq, 100)
scan_lines_harm = gaussian_filter(scan_lines, 1/kgrid.dt, 2 * tone_burst_freq, 30) # plotting was not impl.

# -----------------------------
# Envelope Detection
Expand All @@ -221,6 +221,8 @@ def main():

# Set the desired size of the image
image_size = kgrid.size
harm_img = db(scan_lines_harm.T)
fund_img = db(scan_lines_fund.T)

# Create the axis variables
x_axis = [0, image_size[0] * 1e3] # [mm]
Expand All @@ -235,12 +237,13 @@ def main():
plt.xlabel('Image width [mm]')
plt.ylabel('Depth [mm]')
plt.subplot(1, 3, 2)
plt.imshow(scan_lines_fund, cmap='bone', aspect='auto', extent=[y_axis[0], y_axis[1], x_axis[1], x_axis[0]])
plt.imshow(fund_img, cmap='bone', aspect='auto', extent=[y_axis[0], y_axis[1], x_axis[1], x_axis[0]],
vmax=np.max(fund_img), vmin=np.max(fund_img)-40)
plt.xlabel('Image width [mm]')
plt.title('Fundamental')
plt.yticks([])
plt.subplot(1, 3, 3)
plt.imshow(scan_lines_harm, cmap='bone', aspect='auto',
plt.imshow(harm_img, cmap='bone', aspect='auto', vmax=np.max(harm_img), vmin=np.max(harm_img)-40,
extent=[y_axis[0], y_axis[1], x_axis[1], x_axis[0]])
plt.yticks([])
plt.xlabel('Image width [mm]')
Expand Down
6 changes: 5 additions & 1 deletion kwave/reconstruction/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ def log_compression(signal, cf, normalize=False):
Returns: signal: log-compressed signal
"""
if normalize:
ms = np.max(signal, axis=0)
ms = np.max(signal, axis=-1)
if np.ndim(signal) == 2:
ms = ms[:, np.newaxis]
signal = ms * (np.log10(1 + cf * signal / ms) / np.log10(1 + cf))
else:
signal = np.log10(1 + cf * signal) / np.log10(1 + cf)
return signal

def db(x):
return 20 * np.log10(np.abs(x))

def apodize(distance, aperture, window):
"""
Expand Down
8 changes: 4 additions & 4 deletions kwave/utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def gaussian_filter(signal: Union[np.ndarray, List[float]],
applied to each matrix row.
Args:
signal: Signal to filter
signal: Signal to filter [channel, samples]
Fs: Sampling frequency [Hz]
frequency: Center frequency of filter [Hz]
bandwidth: Bandwidth of filter in percentage
Expand All @@ -384,7 +384,7 @@ def gaussian_filter(signal: Union[np.ndarray, List[float]],
"""

N = len(signal)
N = signal.shape[-1]
if N % 2 == 0:
f = np.arange(-N / 2, N / 2) * Fs / N
else:
Expand All @@ -399,10 +399,10 @@ def gaussian_filter(signal: Union[np.ndarray, List[float]],

# add dimensions to filter to be broadcastable to signal shape
if len(signal.shape) == 2:
gfilter = gfilter[:, np.newaxis]
gfilter = gfilter[np.newaxis,:]

# apply filter
signal = np.real(ifft(ifftshift(gfilter.T * fftshift(fft(signal.T))))).T
signal = np.real(ifft(ifftshift(gfilter * fftshift(fft(signal)))))

return signal

Expand Down

0 comments on commit 78f4ec1

Please sign in to comment.