How to delay a signal using basic operators

4 visualizzazioni (ultimi 30 giorni)
Brian Landers
Brian Landers il 11 Feb 2024
Modificato: John D'Errico il 11 Feb 2024
Hey folks, all I'm trying to do is to create an echo for a signal I've read in the format
y[n] = x[n] - x[n-delta]
where x[n] is the signal gathered via audioread.
I've tried doing such things as:
n = 1:100000
y = x + x(n-10000)
but doing this returns an "array indices must be positive integers" error
and such things as
lag = 0.20
N = round(Fs,lag)
xfr = [x;zeros(N,1)]
xec = [zeros(N,1);x]*alph
that I helpfully found online, but which unhelpfully gave me an "error using vertcat; dimensions of arrays being concatenated are not consistent" error.
I feel like this should be very simple, and I feel extremely stupid and demotivated for not being able to figure out such a basic operation.

Risposte (1)

John D'Errico
John D'Errico il 11 Feb 2024
Modificato: John D'Errico il 11 Feb 2024
This comes down to boundary conditions. So, first, a simple signal...
x = [2 3 5 7 11];
Can't be any simpler than that. Now, suppose you want to perform the simple difference as you wrote? Just lag the vector, then subtract elements. We would have...
y = [x(1)-x(0), x(2) - x(1), x(3) - x(2), x(4) - x(3), x(5) - x(4)]
That seems eminently logical. But wait. What is that first element in the vector? What is x(0)? It does not exist. This is where you are going wrong. It is also why this error message was generated:
"array indices must be positive integers"
Do you see it? x(0) is a problem, since the first element of a vector in MATLAB is x(1).
Effectively, you need to decide what should happen on that boundary. Until you do that, you cannot do what you think you want to do. You might decide the result will be a shorter length than the original signal. For example, diff does exactly that.
y = diff(x)
y = 1×4
1 2 2 4
If you wanted to do this yourself, I'd do this:
y = x(2:end) - x(1:end-1)
y = 1×4
1 2 2 4
Or you might decide to define that boundary in a variety of different ways. But you need to make a choice here. Until than, all you can get is an error message.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by