| 73 | |
| 74 | |
| 75 | class Base: |
| 76 | def __init__(self): |
| 77 | pass |
| 78 | |
| 79 | def spectrogram(self, input, eps=0.): |
| 80 | (real, imag) = self.stft(input) |
| 81 | return torch.clamp(real ** 2 + imag ** 2, eps, np.inf) ** 0.5 |
| 82 | |
| 83 | def spectrogram_phase(self, input, eps=0.): |
| 84 | (real, imag) = self.stft(input) |
| 85 | mag = torch.clamp(real ** 2 + imag ** 2, eps, np.inf) ** 0.5 |
| 86 | cos = real / mag |
| 87 | sin = imag / mag |
| 88 | return mag, cos, sin |
| 89 | |
| 90 | |
| 91 | def wav_to_spectrogram_phase(self, input, eps=1e-10): |
| 92 | """Waveform to spectrogram. |
| 93 | |
| 94 | Args: |
| 95 | input: (batch_size, segment_samples, channels_num) |
| 96 | |
| 97 | Outputs: |
| 98 | output: (batch_size, channels_num, time_steps, freq_bins) |
| 99 | """ |
| 100 | sp_list = [] |
| 101 | cos_list = [] |
| 102 | sin_list = [] |
| 103 | channels_num = input.shape[1] |
| 104 | for channel in range(channels_num): |
| 105 | mag, cos, sin = self.spectrogram_phase(input[:, channel, :], eps=eps) |
| 106 | sp_list.append(mag) |
| 107 | cos_list.append(cos) |
| 108 | sin_list.append(sin) |
| 109 | |
| 110 | sps = torch.cat(sp_list, dim=1) |
| 111 | coss = torch.cat(cos_list, dim=1) |
| 112 | sins = torch.cat(sin_list, dim=1) |
| 113 | return sps, coss, sins |
| 114 | |
| 115 | def wav_to_spectrogram(self, input, eps=0.): |
| 116 | """Waveform to spectrogram. |
| 117 | |
| 118 | Args: |
| 119 | input: (batch_size, segment_samples, channels_num) |
| 120 | |
| 121 | Outputs: |
| 122 | output: (batch_size, channels_num, time_steps, freq_bins) |
| 123 | """ |
| 124 | sp_list = [] |
| 125 | channels_num = input.shape[1] |
| 126 | for channel in range(channels_num): |
| 127 | sp_list.append(self.spectrogram(input[:, channel, :], eps=eps)) |
| 128 | |
| 129 | output = torch.cat(sp_list, dim=1) |
| 130 | return output |
| 131 | |
| 132 |
nothing calls this directly
no outgoing calls
no test coverage detected