Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from bmcfee/time-coding
added time position coding
- Loading branch information
Showing
5 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python | ||
'''Time features''' | ||
|
||
import numpy as np | ||
|
||
from librosa import get_duration | ||
|
||
from .base import FeatureExtractor | ||
|
||
__all__ = ['TimePosition'] | ||
|
||
|
||
class TimePosition(FeatureExtractor): | ||
'''TimePosition: encode frame position as features. | ||
Attributes | ||
---------- | ||
name : str | ||
The name of this feature extractor | ||
sr : number > 0 | ||
The sampling rate of audio | ||
hop_length : int > 0 | ||
The hop length of analysis windows | ||
''' | ||
|
||
def __init__(self, name, sr, hop_length, conv=None): | ||
super(TimePosition, self).__init__(name, sr, hop_length, conv=conv) | ||
|
||
self.register('relative', 2, np.float32) | ||
self.register('absolute', 2, np.float32) | ||
|
||
def transform_audio(self, y): | ||
'''Compute the time position encoding | ||
Parameters | ||
---------- | ||
y : np.ndarray | ||
Audio buffer | ||
Returns | ||
------- | ||
data : dict | ||
data['relative'] = np.ndarray, shape=(n_frames, 2) | ||
data['absolute'] = np.ndarray, shape=(n_frames, 2) | ||
Relative and absolute time positional encodings. | ||
''' | ||
|
||
duration = get_duration(y=y, sr=self.sr) | ||
n_frames = self.n_frames(duration) | ||
|
||
relative = np.zeros((n_frames, 2), dtype=np.float32) | ||
relative[:, 0] = np.cos(np.pi * np.linspace(0, 1, num=n_frames)) | ||
relative[:, 1] = np.sin(np.pi * np.linspace(0, 1, num=n_frames)) | ||
|
||
absolute = relative * np.sqrt(duration) | ||
|
||
return {'relative': relative[self.idx], | ||
'absolute': absolute[self.idx]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters