def __init__(self, biort='farras', qshift='qshift_a', J=3, mode='symmetric'): super().__init__() self.biort = biort self.qshift = qshift self.J = J if isinstance(biort, str): biort = _level1(biort) assert len(biort) == 8 h0a1, h0b1, _, _, h1a1, h1b1, _, _ = biort DWTaa1 = DWTForward(J=1, wave=(h0a1, h1a1, h0a1, h1a1), mode=mode) DWTab1 = DWTForward(J=1, wave=(h0a1, h1a1, h0b1, h1b1), mode=mode) DWTba1 = DWTForward(J=1, wave=(h0b1, h1b1, h0a1, h1a1), mode=mode) DWTbb1 = DWTForward(J=1, wave=(h0b1, h1b1, h0b1, h1b1), mode=mode) self.level1 = nn.ModuleList([DWTaa1, DWTab1, DWTba1, DWTbb1]) if J > 1: if isinstance(qshift, str): qshift = _qshift(qshift) assert len(qshift) == 8 h0a, h0b, _, _, h1a, h1b, _, _ = qshift DWTaa = DWTForward(J - 1, (h0a, h1a, h0a, h1a), mode=mode) DWTab = DWTForward(J - 1, (h0a, h1a, h0b, h1b), mode=mode) DWTba = DWTForward(J - 1, (h0b, h1b, h0a, h1a), mode=mode) DWTbb = DWTForward(J - 1, (h0b, h1b, h0b, h1b), mode=mode) self.level2 = nn.ModuleList([DWTaa, DWTab, DWTba, DWTbb])
def icplxdual2D(yl, yh, level1='farras', qshift='qshift_a', mode='periodization'): # Get the filters _, _, g0a1, g0b1, _, _, g1a1, g1b1 = _level1(level1) _, _, g0a, g0b, _, _, g1a, g1b = _qshift(qshift) dev = yl[0][0].device Faf = ((prep_filt_sfb2d(g0a1, g1a1, g0a1, g1a1, device=dev), prep_filt_sfb2d(g0a1, g1a1, g0b1, g1b1, device=dev)), (prep_filt_sfb2d(g0b1, g1b1, g0a1, g1a1, device=dev), prep_filt_sfb2d(g0b1, g1b1, g0b1, g1b1, device=dev))) af = ((prep_filt_sfb2d(g0a, g1a, g0a, g1a, device=dev), prep_filt_sfb2d(g0a, g1a, g0b, g1b, device=dev)), (prep_filt_sfb2d(g0b, g1b, g0a, g1a, device=dev), prep_filt_sfb2d(g0b, g1b, g0b, g1b, device=dev))) # Convert the highs back to subbands J = len(yh) w = [[[[None for i in range(3)] for j in range(2)] for k in range(2)] for l in range(J)] for j in range(J): w[j][0][0][0], w[j][1][1][0] = pm(yh[j][:,2,:,:,:,0], yh[j][:,3,:,:,:,1]) w[j][0][1][0], w[j][1][0][0] = pm(yh[j][:,3,:,:,:,0], yh[j][:,2,:,:,:,1]) w[j][0][0][1], w[j][1][1][1] = pm(yh[j][:,0,:,:,:,0], yh[j][:,5,:,:,:,1]) w[j][0][1][1], w[j][1][0][1] = pm(yh[j][:,5,:,:,:,0], yh[j][:,0,:,:,:,1]) w[j][0][0][2], w[j][1][1][2] = pm(yh[j][:,1,:,:,:,0], yh[j][:,4,:,:,:,1]) w[j][0][1][2], w[j][1][0][2] = pm(yh[j][:,4,:,:,:,0], yh[j][:,1,:,:,:,1]) w[j][0][0] = torch.stack(w[j][0][0], dim=2) w[j][0][1] = torch.stack(w[j][0][1], dim=2) w[j][1][0] = torch.stack(w[j][1][0], dim=2) w[j][1][1] = torch.stack(w[j][1][1], dim=2) y = None for m in range(2): for n in range(2): lo = yl[m][n] for j in range(J-1, 0, -1): lo = sfb2d(lo, w[j][m][n], af[m][n], mode=mode) lo = sfb2d(lo, w[0][m][n], Faf[m][n], mode=mode) # Add to the output if y is None: y = lo else: y = y + lo # Normalize y = y/2 return y
def __init__(self, biort='farras', qshift='qshift_a', mode='symmetric'): super().__init__() self.biort = biort self.qshift = qshift if isinstance(biort, str): biort = _level1(biort) assert len(biort) == 8 _, _, g0a1, g0b1, _, _, g1a1, g1b1 = biort IWTaa1 = DWTInverse(wave=(g0a1, g1a1, g0a1, g1a1), mode=mode) IWTab1 = DWTInverse(wave=(g0a1, g1a1, g0b1, g1b1), mode=mode) IWTba1 = DWTInverse(wave=(g0b1, g1b1, g0a1, g1a1), mode=mode) IWTbb1 = DWTInverse(wave=(g0b1, g1b1, g0b1, g1b1), mode=mode) self.level1 = nn.ModuleList([IWTaa1, IWTab1, IWTba1, IWTbb1]) if isinstance(qshift, str): qshift = _qshift(qshift) assert len(qshift) == 8 _, _, g0a, g0b, _, _, g1a, g1b = qshift IWTaa = DWTInverse(wave=(g0a, g1a, g0a, g1a), mode=mode) IWTab = DWTInverse(wave=(g0a, g1a, g0b, g1b), mode=mode) IWTba = DWTInverse(wave=(g0b, g1b, g0a, g1a), mode=mode) IWTbb = DWTInverse(wave=(g0b, g1b, g0b, g1b), mode=mode) self.level2 = nn.ModuleList([IWTaa, IWTab, IWTba, IWTbb])
def cplxdual2D(x, J, level1='farras', qshift='qshift_a', mode='periodization', mag=False): """ Do a complex dtcwt Returns: lows: lowpass outputs from each of the 4 trees. Is a 2x2 list of lists w: bandpass outputs from each of the 4 trees. Is a list of lists, with shape [J][2][2][3]. Initially the 3 outputs are the lh, hl and hh from each of the 4 trees. After doing sums and differences though, they become the real and imaginary parts for the 6 orientations. In particular: first index - indexes over scales second index - 0 = real, 1 = imaginary third and fourth indices: 0,1 = 15 degrees 1,2 = 45 degrees 0,0 = 75 degrees 1,0 = 105 degrees 0,2 = 135 degrees 1,1 = 165 degrees """ x = x / 2 # Get the filters h0a1, h0b1, _, _, h1a1, h1b1, _, _ = _level1(level1) h0a, h0b, _, _, h1a, h1b, _, _ = _qshift(qshift) Faf = ((prep_filt_afb2d(h0a1, h1a1, h0a1, h1a1, device=x.device), prep_filt_afb2d(h0a1, h1a1, h0b1, h1b1, device=x.device)), (prep_filt_afb2d(h0b1, h1b1, h0a1, h1a1, device=x.device), prep_filt_afb2d(h0b1, h1b1, h0b1, h1b1, device=x.device))) af = ((prep_filt_afb2d(h0a, h1a, h0a, h1a, device=x.device), prep_filt_afb2d(h0a, h1a, h0b, h1b, device=x.device)), (prep_filt_afb2d(h0b, h1b, h0a, h1a, device=x.device), prep_filt_afb2d(h0b, h1b, h0b, h1b, device=x.device))) # Do 4 fully decimated dwts w = [[[None for _ in range(2)] for _ in range(2)] for j in range(J)] lows = [[None for _ in range(2)] for _ in range(2)] for m in range(2): for n in range(2): # Do the first level transform with the first level filters # ll, bands = afb2d(x, (Faf[m][0], Faf[m][1], Faf[n][0], Faf[n][1]), mode=mode) bands = afb2d(x, Faf[m][n], mode=mode) # Separate the low and bandpasses s = bands.shape bands = bands.reshape(s[0], -1, 4, s[-2], s[-1]) ll = bands[:, :, 0].contiguous() w[0][m][n] = [bands[:, :, 2], bands[:, :, 1], bands[:, :, 3]] # Do the second+ level transform with the second level filters for j in range(1, J): # ll, bands = afb2d(ll, (af[m][0], af[m][1], af[n][0], af[n][1]), mode=mode) bands = afb2d(ll, af[m][n], mode=mode) # Separate the low and bandpasses s = bands.shape bands = bands.reshape(s[0], -1, 4, s[-2], s[-1]) ll = bands[:, :, 0].contiguous() w[j][m][n] = [bands[:, :, 2], bands[:, :, 1], bands[:, :, 3]] lows[m][n] = ll # Convert the quads into real and imaginary parts yh = [ None, ] * J for j in range(J): deg75r, deg105i = pm(w[j][0][0][0], w[j][1][1][0]) deg105r, deg75i = pm(w[j][0][1][0], w[j][1][0][0]) deg15r, deg165i = pm(w[j][0][0][1], w[j][1][1][1]) deg165r, deg15i = pm(w[j][0][1][1], w[j][1][0][1]) deg135r, deg45i = pm(w[j][0][0][2], w[j][1][1][2]) deg45r, deg135i = pm(w[j][0][1][2], w[j][1][0][2]) yhr = torch.stack((deg15r, deg45r, deg75r, deg105r, deg135r, deg165r), dim=1) yhi = torch.stack((deg15i, deg45i, deg75i, deg105i, deg135i, deg165i), dim=1) if mag: yh[j] = torch.sqrt(yhr**2 + yhi**2 + 0.01) - np.sqrt(0.01) else: yh[j] = torch.stack((yhr, yhi), dim=-1) return lows, yh