How to delay a signal using basic operators

4 次查看(过去 30 天)
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.

回答(1 个)

John D'Errico
John D'Errico 2024-2-11
编辑:John D'Errico 2024-2-11
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.

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by