If you follow the code of audiowrite, the floating point array is never converted and is passed as is to asyncioimpl.OutputStream.write. That latter method, you can't see the code so it must be compiled code, not m-file, probably as part of audiofilesndfilewriterplugin.dll. Most likely, it's C code or similar and follow C rules for conversions from float to integer. The C rule truncates floating point values (rounds towards 0).
Your cast on the other hand uses matlab float to integer rules which is rule to nearest. Hence the discrepancy.
You can emulate the C rules if you wish (for audio, matlab rules are probably better) using fix.
audiowrite('y.wav', int16(fix(y * 2^15)), 44100)
audiowrite('y.wave', y, 44100)
Bottom line, matlab and C (and many other languages) don't follow the same rules when dealing with integer types. There's similar unexpected behaviour (for a C programmer) when dealing with integer division (matlab rounds by default, C truncates).
Note that you don't need to use cast to do the conversion to integer.
Best Answer