MATLAB Answers

What exactly is happening in this line of code speech_out = speech(1:1​/1.01964:l​ength(spee​ch),:);?

1 view (last 30 days)
Christopher Kelton
Christopher Kelton on 24 Nov 2019
Commented: dpb on 24 Nov 2019
I'm trying to make an incoming signal of a desired length in order to perform speech recognition calculations on it in a similar manner to every incoming signal. I originally tried interpolating and decimating to get to desired length, but that would result in a vector with every element being 0, probably because it had to run through the decimation process so many times.
Matlab Code for Desired Signal Length.PNG
And it seemed like this would accomplish what I wanted in the first place, stretching or shrinking the signal array whilst keeping the form of the signal.
I understand the following inputs:
speech_out = speech(1:2:length(speech),:); will delete every other row starting with the second element.
speech_out = speech(1:1/2:length(speech),:); will duplicate every other row starting with the second element.
speech_out = speech(1:1/1.25:length(speech),:); will duplicate every fourth row starting after the newly duplicated element.
This is essentially multiplying the length of the array by the inverse of the 2nd input (e.g. 2, 1/2, 1/1.25, etc...) and results in the correctly sized signal and seems to keep the form of the signal.
Code used:
desired_length = 89728;
speech_out = speech(1:(1/(desired_length/length(speech))):length(speech),:); %iso_sig
BEFORE CODE: (After taking out the silences from the signal)
Signal After Taking out Zeros and Before Making Desired Length.PNG
AFTER CODE: (Now of correct size, while seemingly retaining the important information)
Signal Of Correct Length.PNG
What I don't understand is when it starts to come down to weirder fractions like 1/1.01964, where it swings between duplicating every 51st, 52nd, or 53rd row (might be a larger range, but this is just what I noticed after scrolling through some of the array). I would like to know how it determines which row to duplicate, because I'm eventually going to want to move this code to C to program a DSP.
*Bonus: I also think that this may be screwing up my end result 256pt-FFT, because every window of FFT shows the same form for whatever signal I put through it.
256-pt FFT of signal.PNG
2nd 256-pt FFT of signal.PNG
This is showing the dominant frequencies to consistently be at Fs/2 and Fs, where my Fs = 44kHz. I was thinking that the above function I was asking about is somehow affecting my FFT, but I don't see why it would be if my signal is still keeping its form.

  0 Comments

Sign in to comment.

Answers (1)

dpb
dpb on 24 Nov 2019
MATLAB rounds to nearest integer for the fractional subscript for indexing. Not recommended technique, use the Signnal Processing TB resample function instead.

  2 Comments

Christopher Kelton
Christopher Kelton on 24 Nov 2019
Thank you! The resample function looks to work similarily in application. Extending from that, would I be correct in my thinking that resample(file,P_factor,Q_factor) is interpolating the file by a factor of P_factor and then decimating the file by a factor of Q_factor? For my application, P_factor would be my desired length and Q_factor would be the current length of my file. This seems like a lot of calculations just to achieve a seemingly minimal task. Do you know of other methods out there that would be less computationaly intensive?
dpb
dpb on 24 Nov 2019
Not exactly. resample "applies an antialiasing FIR lowpass filter to x and compensates for the delay introduced by the filter." If you're doing spectral work with the signal, this can be important.
If you don't care about such niceties, there's always interp1 or resample with the explicit timestep input option available.

Sign in to comment.

Sign in to answer this question.