diff --git a/examples/bmode_reconstruction_example.py b/examples/bmode_reconstruction_example.py index 16073ffb..8a9707ca 100644 --- a/examples/bmode_reconstruction_example.py +++ b/examples/bmode_reconstruction_example.py @@ -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 @@ -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 @@ -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] @@ -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]') diff --git a/kwave/reconstruction/tools.py b/kwave/reconstruction/tools.py index 3101190d..b05b92ca 100644 --- a/kwave/reconstruction/tools.py +++ b/kwave/reconstruction/tools.py @@ -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): """ diff --git a/kwave/utils/filters.py b/kwave/utils/filters.py index 660a3dc4..2c814e1f 100644 --- a/kwave/utils/filters.py +++ b/kwave/utils/filters.py @@ -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 @@ -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: @@ -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