How to create an array of sine waves from arrays of parameters

36 views (last 30 days)
I have three Nx1 arrays of parameters: V, f, φ and a 1xM array t, which I must use to create a NxM array of sine waves V*sin(2π*f+φ).
Having experience with functional programming my initial strategy was to use a map function with an anonymous function to map the given parameters. While Matlab doesn't have a function with that name, I read that arrayfun might be what I am looking for but can't figure out how to use it.
I tried several variations to no success, any hints?

Answers (1)

Star Strider
Star Strider on 13 Sep 2018
I would use a combination of vector multiplication of ‘f’ and ‘t’, and bsxfun calls:
N = 5; % Create Data
M = 250;
V = randi(10, N, 1);
f = randi(10, N, 1);
psi = rand(N, 1)*2*pi;
t = linspace(0, 10, M); % Create Data
sine_mtx = bsxfun(@times, sin(bsxfun(@plus, 2*pi*f*t, psi)), V);
figure
plot(t, sine_mtx)
grid

Community Treasure Hunt

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

Start Hunting!