Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: lossless (or so) encoding of square wave (Read 4457 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

lossless (or so) encoding of square wave

I want to do something, but I am not sure it's possible.
Here is the problem:

1) I have a portable mp3/asf/wma player
2) I have a WAV that contains a square wave - sound that encodes the data for the old 8-bit personal computer (who used ZX Spectrum or such knows what am I talking about) - file full of zeros and 0x7FFF values.

My player can play only the specified formats, no WAV or something else.
When I encode WAV to MP3, even using bitrate=256, it breaks the sound and the data can't be loaded into the computer.

My question is: can I encode WAV to MP3 (or ASF, or WMA) in more-or-less lossless way so the sound will stay intact enough ? The size of resulting file doesn't matter.
What matters is the file should be playable on my portable.

10x in advance for help.

lossless (or so) encoding of square wave

Reply #1
You want to load data into the ZX Spectrum, from the sound (treating it as data)?

It's not really possible, because mp3/wma/asf will remove *inaudible* differences, and in your case it will remove a lot of actual data.

You can try --alt-preset insane (cbr 320) , and if that doesn't help nothing will

Perhaps try --freeformat if your portable supports it?

WMA has a lossless mode but I'm not sure if any portable can play such files yet.

Edit: audible -> inaudible

lossless (or so) encoding of square wave

Reply #2
Thanks a lot !

cbr 320  and -q 0 work stable.
Maybe 256 will work too - I will check it again.

lossless (or so) encoding of square wave

Reply #3
Quote
It's not really possible, because mp3/wma/asf will remove *audible* differences, and in your case it will remove a lot of actual data.

(I assume you meant "inaudible").
This isn't quite correct. The mp3/wma/asf formats themselves can hold pretty much any audio wave (within their respective limits of course). You can perfectly write an encoder which is optimized to reproduce square waves well, without any psychoacoustic concerns.

Then the result at 32kbps could very well produce more accurate results than LAME CBR 320, because it focuses on reproducing "accurate square waves" not "good sound" or "flat frequency response".

Quote
You can try --alt-preset insane (cbr 320) , and if that doesn't help nothing will


Incorrect again. First of all, the signal should get a good deal of denoising. This can remove A LOT of spurious high-frequency data. Then it can probably be lowpassed (with a linear-phase FIR filter). Then the psymodel should definitely be disabled (using --no-psymodel IIRC) to save space and improve accuracy.

If lowpassing is too destructive, then nspsytune switches can be used to spare bits on high frequencies.

I hear it works at 320CBR, and probably it still can at 128 or 160 (depending at what rate the ZX records data).

If you can lower the bitrate, let us know 

Regards

lossless (or so) encoding of square wave

Reply #4
Such a signal is bound to cause serious clipping trouble. Try the --scale switch (e.g. lame --alt-preset standard --scale 0.5) and experiment with different values. "1" is fullscale while "0" should give you a silent mp3.

I believe mp3 suffers encoder side clipping to some degree, so replaygaining the file after encoding might not be enough. Maybe lowpassing beforehand will enable you to use a scale value closer to 1 and give more reliable results when reading the data.

Btw: -q 0 is not recommended.

lossless (or so) encoding of square wave

Reply #5
Quote
(I assume you meant "inaudible").


Yup...

Quote
Quote
You can try --alt-preset insane (cbr 320) , and if that doesn't help nothing will


Incorrect again. First of all, the signal should get a good deal of denoising. This can remove A LOT of spurious high-frequency data. Then it can probably be lowpassed (with a linear-phase FIR filter). Then the psymodel should definitely be disabled (using --no-psymodel IIRC) to save space and improve accuracy.


But the signal was clean and had only "zeros and 0x7FFF values".

There should be no need for any denoising, lowpassing will only make the square wave less square.

Signals like this (consisting of mostly transients) will cause a lot of pre-echo, which I think is handled better using a higher bitrate.

My understanding was that cax wanted to play some data files as sound, so that his ZX Spectrum could load the data from the sound played from his portable.

lossless (or so) encoding of square wave

Reply #6
See this thread:
http://www.hydrogenaudio.org/forums/index.php?showtopic=6346

Also, you could consider trying a real lossless compressor (FLAC, Monkey's Audio or LA for example), as this is a very strange signal that might be compressed very well by a lossless compressor (I'd try a few and see which works best).

lossless (or so) encoding of square wave

Reply #7
IIRC, the reason that your data is getting broken is as a result of the Gibbs Phenomenon.
Quote
Immediately on each side of a discontinuity a Fourier series exhibits a certain behaviour known as the Gibbs phenomenon.
If we calculate values using a truncated version of the series we find that the results exhibit an oscillatory behaviour on each side of the discontinuity.


Basically what this means is that it is very hard (or at least bitrate intensive) for any discrete Fourier transform (or similar transform) based codec to encode a square wave. This includes MP3, AAC and Vorbis and I would assume WMA and most others.

It will be fairly straight forward to filter this out with a simple program on your Spectrum. Just write a piece of code which reads in the binary data and sets every word to 0x7FFF or 0x0 depending on which it is closest to. You will find that you can extract a perfect wave form from a fairly low bitrate MP3.

You could also look at other coding methods (other than straight binary) that MP3 won't munge so much. For example, you could try something like Manchester coding.

lossless (or so) encoding of square wave

Reply #8
Thanks to all who tried to help !

I changed the program that created square wave so it produces a sine-alike wave.
Now I am able to encode at 128 kbps without breaking the data
(was 320 kbps for square wave).

I continue to play with the waveform because I know it's not the limit - I have another data->wav conversion tool which produces WAVs that don't break even at 32 kbps !

Another positive effect I got from changing square to sine is that now if I burn WAV file to audio CD, it works more stable when played in diskman. Looks like diskmans "like" sine waves more than square ones. Has this effect any physical explanation ?

lossless (or so) encoding of square wave

Reply #9
Since you've said the software generating the ZX spectrum signal can be programmed to generate something other than a square wave, could you try this? ...

Instead of a square wave, generate a sine wave. Same frequency (the frequency varies, doesn't it? - well, same frequencies), but pure sine wave. Not even "sine alike" - pure sine wave.

Encode that. It should be very easy. You might be able to use a very low bitrate. (btw, make sure it's mono).

Now, use mp3gain to boost the volume by a large amount, e.g. +20dB.

Result (upon decoding in your player): one heavily clipped sine wave, which is almost identical to a square wave.

Well, it might work!

Cheers,
David.