I have an array of bits, such as the one below, and I need to put a carrier wave on top of them. The question is: Is this possible to do without looping?
For example, suppose you are using Frequency-Shift-Keying. If the bit is a “1” then the signal should be a sine wave with a frequency of 10,000 Hz, and if the bit is a “0” then the signal should be a sine wave with a frequency of 8,000 Hz.
Bit-Array:
bits = [0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1]
Looping Code:
for i = 1:length(bits)
if bits(i) == 1
signal = [signal sin(2*pi*10000*t)]
else
signal = [signal sin(2*pi*8000*t)]
end
end
It would be nice if I could perform this all in a single operation with no looping.
Thanks.
If
tis a scalar, then you can replace your for loop with a single-line vectorized solution:However, if you’re doing frequency-shift keying, it seems like you should be expanding each
0or1in yourbitsvector into a sine wave with a given duration and frequency. For example, to create a modulated signal with 4 sine periods (0.1 msec) at 8 kHz for each0and 5 sine periods (0.1 msec) at 10 kHz for each1, you can use the function KRON like so: