double to int16 (generation or conversion?)
fsamp = 2;
deltaf = fsamp/nfft; % FFT resolution
Nfreqtimestwo = 128; % Used below
Nsines = Nfreqtimestwo/2 - 1; % Number of sine waves
fmult = [1:Nsines]; % multiplicative factor
freq_fund = fsamp/Nfreqtimestwo;
freq_sines = freq_fund.*fmult;
omega = 2*pi*freq_sines;
r = int16(0);
for(ii=1:Nsines)
r = r + cos((omega(ii)/fsamp)*(0:messageLen-1));
end
This is the code I am currently using to create my input signal. However, the end result of r
is a 32,768 array of doubles. Now I would like to do the best approximation of that using int16
. However, I would like to note that amplitude doesn't really matter. For example, my best approach so far I think has been:
fsamp = 2;
deltaf = fsamp/nfft; % FFT开发者_如何学运维 resolution
Nfreqtimestwo = 128; % Used below
Nsines = Nfreqtimestwo/2 - 1; % Number of sine waves
fmult = [1:Nsines]; % multiplicative factor
freq_fund = fsamp/Nfreqtimestwo;
freq_sines = freq_fund.*fmult;
omega = 2*pi*freq_sines;
r = int16(0);
for(ii=1:Nsines)
r = r + int16(8192*cos((omega(ii)/fsamp)*(0:messageLen-1)));
end
Are there any better ways to approach this?
EDIT The reason I want to convert the doubles to ints is because this list is being used in an embedded system and eventually going to a 16-bit DAC... no doubles allowed
int16(vector)
converts vector
from double
to int16
and this is the preferred way. The alternate way of doing it is to define all your constants as int16
s in which case, MATLAB will give you the result as an int16
. However, this is cumbersome, so stick with what you have (unless if you absolutely have to do it this way).
Also, unrelated to your actual question, you can ditch the loop by using cumsum
. I'll leave that for you to try out :)
精彩评论