def idctii(x, axes=None):
"""
Compute a multi-dimensional inverse DCT-II over specified array axes.
This function is implemented by calling the one-dimensional inverse
DCT-II :func:`scipy.fftpack.idct` with normalization mode 'ortho'
for each of the specified axes.
Parameters
----------
a : array_like
Input array
axes : sequence of ints, optional (default None)
Axes over which to compute the inverse DCT-II.
Returns
-------
y : ndarray
Inverse DCT-II of input array
"""
if axes is None:
axes = list(range(x.ndim))
for ax in axes[::-1]:
x = fftpack.idct(x, type=2, axis=ax, norm='ortho')
return x
python类idct()的实例源码
def dct_uncompress(X_compressed, window_size=128):
"""
Uncompress a DCT compressed signal (such as returned by ``compress``).
Parameters
----------
X_compressed : ndarray, shape=(n_samples, n_features)
Windowed and compressed array.
window_size : int, optional (default=128)
Size of the window used when ``compress`` was called.
Returns
-------
X_reconstructed : ndarray, shape=(n_samples)
Reconstructed version of X.
"""
if X_compressed.shape[1] % window_size != 0:
append = np.zeros((X_compressed.shape[0],
window_size - X_compressed.shape[1] % window_size))
X_compressed = np.hstack((X_compressed, append))
X_r = fftpack.idct(X_compressed, norm='ortho')
return X_r.ravel()
def overlap_dct_uncompress(X_compressed, window_size):
"""
Uncompress X as returned from ``overlap_compress``.
Parameters
----------
X_compressed : ndarray, shape=(n_windows, n_components)
Windowed and compressed version of X
window_size : int
Size of windows originally used when compressing X
Returns
-------
X_reconstructed : ndarray, shape=(n_samples,)
Reconstructed version of X
"""
if X_compressed.shape[1] % window_size != 0:
append = np.zeros((X_compressed.shape[0], window_size -
X_compressed.shape[1] % window_size))
X_compressed = np.hstack((X_compressed, append))
X_r = fftpack.idct(X_compressed, norm='ortho')
return invert_halfoverlap(X_r)
def run_fft_dct_example():
random_state = np.random.RandomState(1999)
fs, d = fetch_sample_speech_fruit()
n_fft = 64
X = d[0]
X_stft = stft(X, n_fft)
X_rr = complex_to_real_view(X_stft)
X_dct = fftpack.dct(X_rr, axis=-1, norm='ortho')
X_dct_sub = X_dct[1:] - X_dct[:-1]
std = X_dct_sub.std(axis=0, keepdims=True)
X_dct_sub += .01 * std * random_state.randn(
X_dct_sub.shape[0], X_dct_sub.shape[1])
X_dct_unsub = np.cumsum(X_dct_sub, axis=0)
X_idct = fftpack.idct(X_dct_unsub, axis=-1, norm='ortho')
X_irr = real_to_complex_view(X_idct)
X_r = istft(X_irr, n_fft)[:len(X)]
SNR = 20 * np.log10(np.linalg.norm(X - X_r) / np.linalg.norm(X))
print(SNR)
wavfile.write("fftdct_orig.wav", fs, soundsc(X))
wavfile.write("fftdct_rec.wav", fs, soundsc(X_r))
def dct_uncompress(X_compressed, window_size=128):
"""
Uncompress a DCT compressed signal (such as returned by ``compress``).
Parameters
----------
X_compressed : ndarray, shape=(n_samples, n_features)
Windowed and compressed array.
window_size : int, optional (default=128)
Size of the window used when ``compress`` was called.
Returns
-------
X_reconstructed : ndarray, shape=(n_samples)
Reconstructed version of X.
"""
if X_compressed.shape[1] % window_size != 0:
append = np.zeros((X_compressed.shape[0],
window_size - X_compressed.shape[1] % window_size))
X_compressed = np.hstack((X_compressed, append))
X_r = fftpack.idct(X_compressed, norm='ortho')
return X_r.ravel()
def overlap_dct_uncompress(X_compressed, window_size):
"""
Uncompress X as returned from ``overlap_compress``.
Parameters
----------
X_compressed : ndarray, shape=(n_windows, n_components)
Windowed and compressed version of X
window_size : int
Size of windows originally used when compressing X
Returns
-------
X_reconstructed : ndarray, shape=(n_samples,)
Reconstructed version of X
"""
if X_compressed.shape[1] % window_size != 0:
append = np.zeros((X_compressed.shape[0], window_size -
X_compressed.shape[1] % window_size))
X_compressed = np.hstack((X_compressed, append))
X_r = fftpack.idct(X_compressed, norm='ortho')
return invert_halfoverlap(X_r)
def decode(self, target):
target.fill(0.)
original_shape = target.shape
target = target.ravel()
for gene in self.genes:
target[gene.index] = gene.value
target = target.reshape(original_shape)
len_shape = len(original_shape)
kwargs = dict(norm='ortho')
if len_shape == 1:
out = idct(target, **kwargs)
elif len_shape == 2:
out = idct(idct(target.T, **kwargs).T, **kwargs)
elif len_shape >= 3:
shape = (np.prod(original_shape[:-1]), original_shape[-1])
target = target.reshape(shape)
out = idct(idct(target.T, **kwargs).T, **kwargs)
out = out.reshape(original_shape)
return out
def run_world_dct_example():
# on chromebook
# enc 114.229
# synth 5.165
fs, d = fetch_sample_speech_tapestry()
d = d.astype("float32") / 2 ** 15
def enc():
temporal_positions_h, f0_h, vuv_h, f0_candidates_h = harvest(d, fs)
temporal_positions_ct, spectrogram_ct, fs_ct = cheaptrick(d, fs,
temporal_positions_h, f0_h, vuv_h)
temporal_positions_d4c, f0_d4c, vuv_d4c, aper_d4c, coarse_aper_d4c = d4c(d, fs,
temporal_positions_h, f0_h, vuv_h)
return spectrogram_ct, f0_d4c, vuv_d4c, coarse_aper_d4c
start = time.time()
spectrogram_ct, f0_d4c, vuv_d4c, coarse_aper_d4c = enc()
dct_buf = fftpack.dct(spectrogram_ct)
n_fft = 512
n_dct = 20
dct_buf = dct_buf[:, :n_dct]
idct_buf = np.zeros((dct_buf.shape[0], n_fft + 1))
idct_buf[:, :n_dct] = dct_buf
ispectrogram_ct = fftpack.idct(idct_buf)
enc_done = time.time()
y = world_synthesis(f0_d4c, vuv_d4c, coarse_aper_d4c, spectrogram_ct, fs)
synth_done = time.time()
print("enc time: {}".format(enc_done - start))
print("synth time: {}".format(synth_done - enc_done))
#y = world_synthesis(f0_d4c, vuv_d4c, aper_d4c, sp_r, fs)
wavfile.write("out_dct.wav", fs, soundsc(y))
#run_world_mgc_example()
#run_world_base_example()
def conv_from_dct(blocks, in_blocksize, out_blocksize):
new_blocks=[]
zeropad = [0]*(out_blocksize-in_blocksize)
dct_pred = blocks
dct_pred = np.append(dct_pred, zeropad)
dct_pred = np.asarray(idct(dct_pred, norm='ortho'), dtype=np.float32)
new_blocks.append(dct_pred)
return new_blocks
def dct_gen(frame):
# read Frame
[r, c, d] = frame.shape
framebw = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
########## DCT ##########
# Reshape in 8x8 type array
framedct = np.reshape(framebw / 255.0, (-1, 8), order='C')
# Apply DCT line and column wise
X = sft.dct(framedct, axis=1, norm='ortho')
X = np.reshape(X, (-1, c), order='C')
X = np.reshape(X.T, (-1, 8), order='C')
X = sft.dct(X, axis=1, norm='ortho')
# shape back to original shape
X = (np.reshape(X, (-1, r), order='C')).T
# Zeroing DC-Coefficients
X[::8,::8] = 0
########## IDCT ##########
# Reshape in 8x8 type array
X = np.reshape(X, (-1, 8), order='C')
# Apply IDCT line and column wise
X = sft.idct(X, axis=1, norm='ortho')
X = np.reshape(X, (-1, c), order='C')
X = np.reshape(X.T, (-1, 8), order='C')
x = sft.idct(X, axis=1, norm='ortho')
# shape back to original shape
x = (np.reshape(x, (-1, r), order='C')).T
# cast into output image
x = x*255
frameout = np.zeros_like(frame)
frameout[:, :, 0] = x
frameout[:, :, 1] = x
frameout[:, :, 2] = x
return frameout
def idct2(image_channel):
return fftpack.idct(fftpack.idct(image_channel.T, norm='ortho').T, norm='ortho')