Skip to content

Commit

Permalink
formating changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xserra committed Aug 27, 2022
1 parent aa132f9 commit 4e7fd73
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 34 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions .idea/sms-tools.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/webResources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 32 additions & 34 deletions software/transformations/harmonicTransformations.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# transformations applied to the harmonics of a sound

import numpy as np
from scipy.signal import resample
from scipy.interpolate import interp1d


def harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreservation, fs):
"""
"""
Frequency scaling of the harmonics of a sound
hfreq, hmag: frequencies and magnitudes of input harmonics
freqScaling: scaling factors, in time-value pairs (value of 1 no scaling)
Expand All @@ -14,35 +14,33 @@ def harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreserva
fs: sampling rate of input sound
returns yhfreq, yhmag: frequencies and magnitudes of output harmonics
"""
if (freqScaling.size % 2 != 0): # raise exception if array not even length
raise ValueError("Frequency scaling array does not have an even size")

if (freqStretching.size % 2 != 0): # raise exception if array not even length
raise ValueError("Frequency stretching array does not have an even size")

L = hfreq.shape[0] # number of frames
nHarms = hfreq.shape[1] # number of harmonics
# create interpolation object with the scaling values
freqScalingEnv = np.interp(np.arange(L), L*freqScaling[::2]/freqScaling[-2], freqScaling[1::2])
# create interpolation object with the stretching values
freqStretchingEnv = np.interp(np.arange(L), L*freqStretching[::2]/freqStretching[-2], freqStretching[1::2])
yhfreq = np.zeros_like(hfreq) # create empty output matrix
yhmag = np.zeros_like(hmag) # create empty output matrix
for l in range(L): # go through all frames
ind_valid = np.where(hfreq[l,:]!=0)[0] # check if there are frequency values
if ind_valid.size == 0: # if no values go to next frame
continue
if (timbrePreservation == 1) & (ind_valid.size > 1): # create spectral envelope
# values of harmonic locations to be considered for interpolation
x_vals = np.append(np.append(0, hfreq[l,ind_valid]),fs/2)
# values of harmonic magnitudes to be considered for interpolation
y_vals = np.append(np.append(hmag[l,0], hmag[l,ind_valid]),hmag[l,-1])
specEnvelope = interp1d(x_vals, y_vals, kind = 'linear',bounds_error=False, fill_value=-100)
yhfreq[l,ind_valid] = hfreq[l,ind_valid] * freqScalingEnv[l] # scale frequencies
yhfreq[l,ind_valid] = yhfreq[l,ind_valid] * (freqStretchingEnv[l]**ind_valid) # stretch frequencies
if (timbrePreservation == 1) & (ind_valid.size > 1): # if timbre preservation
yhmag[l,ind_valid] = specEnvelope(yhfreq[l,ind_valid]) # change amplitudes to maintain timbre
else:
yhmag[l,ind_valid] = hmag[l,ind_valid] # use same amplitudes as input
return yhfreq, yhmag

if (freqScaling.size % 2 != 0): # raise exception if array not even length
raise ValueError("Frequency scaling array does not have an even size")

if (freqStretching.size % 2 != 0): # raise exception if array not even length
raise ValueError("Frequency stretching array does not have an even size")

L = hfreq.shape[0] # number of frames
# create interpolation object with the scaling values
freqScalingEnv = np.interp(np.arange(L), L * freqScaling[::2] / freqScaling[-2], freqScaling[1::2])
# create interpolation object with the stretching values
freqStretchingEnv = np.interp(np.arange(L), L * freqStretching[::2] / freqStretching[-2], freqStretching[1::2])
yhfreq = np.zeros_like(hfreq) # create empty output matrix
yhmag = np.zeros_like(hmag) # create empty output matrix
for l in range(L): # go through all frames
ind_valid = np.where(hfreq[l, :] != 0)[0] # check if there are frequency values
if ind_valid.size == 0: # if no values go to next frame
continue
if (timbrePreservation == 1) & (ind_valid.size > 1): # create spectral envelope
# values of harmonic locations to be considered for interpolation
x_vals = np.append(np.append(0, hfreq[l, ind_valid]), fs / 2)
# values of harmonic magnitudes to be considered for interpolation
y_vals = np.append(np.append(hmag[l, 0], hmag[l, ind_valid]), hmag[l, -1])
specEnvelope = interp1d(x_vals, y_vals, kind='linear', bounds_error=False, fill_value=-100)
yhfreq[l, ind_valid] = hfreq[l, ind_valid] * freqScalingEnv[l] # scale frequencies
yhfreq[l, ind_valid] = yhfreq[l, ind_valid] * (freqStretchingEnv[l] ** ind_valid) # stretch frequencies
if (timbrePreservation == 1) & (ind_valid.size > 1): # if timbre preservation
yhmag[l, ind_valid] = specEnvelope(yhfreq[l, ind_valid]) # change amplitudes to maintain timbre
else:
yhmag[l, ind_valid] = hmag[l, ind_valid] # use same amplitudes as input
return yhfreq, yhmag

0 comments on commit 4e7fd73

Please sign in to comment.