Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct SpectralFlux #1

Open
SiggiGue opened this issue Mar 10, 2017 · 0 comments
Open

Correct SpectralFlux #1

SiggiGue opened this issue Mar 10, 2017 · 0 comments

Comments

@SiggiGue
Copy link
Owner

Something like this (multichannels not supported in this example)

class SpectralFlux(Feature):
    axis = Parameter(0)
    fmin = Parameter()
    fmax = Parameter()
    
    def requires(self):
        yield AbsRfft

    def on_start(self, source, featureset, sink):
        nfft = featureset['AbsRfft'].nfft
        freqs = featureset['AbsRfft'].frequencies
        fmin, fmax = self.fmin, self.fmax
        if fmin and fmax:
            self._index = np.logical_and(freqs >=fmin, freqs < fmax)
            nfft = np.sum(self._index)
        else: 
            self._index = self._lastspec > 0
        
        if source.channels > 1:
            self._lastspec = np.ones((nfft, source.channels))
        else:
            self._lastspec = np.ones(nfft)

        if not any(self._index):
            raise ValueError('Could not find frequency bins between {} and {} Hz'.format(fmin, fmax))
                    
    def process(self, data, featuredata):
        curspec = featuredata['AbsRfft'][self._index]
        ds = curspec - self._lastspec
        specflux = np.sum((ds + np.abs(ds)) * 0.5)
        self._lastspec = curspec
        return specflux
    
    @classmethod
    def gen_by_edge_frequencies(cls, edgefreqs, prefixname='SpectralFlux'):
        for le, ue in zip(edgefreqs[:-1], edgefreqs[1:]):
            yield cls(name='{}{}to{}Hz'.format(cls.__name__, le, ue), fmin=le, fmax=ue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant