EOJ 1918/POJ 3444/Rocky Mountain 2007 Wavelet Compression

题目简介


The discrete wavelet transform is a popular tool for signal compression. In this problem, your job is to write a program to decompress a one-dimensional signal (a list of integers) that has been compressed by a simple wavelet transform.

To understand how this simple wavelet transform works, suppose that we have a list of an even number of integers. We compute the sum and difference of each pair of consecutive samples, resulting in two lists of sums and differences each having half the original length. Formally, if the original samples are

a(1),…, a(n)
the i-th sum s(i) and difference d(i)are computed as:

for i = 1,…,n/2:

s(i) = a(2*i-1) + a(2*i)

d(i) = a(2*i-1) - a(2*i)
This is then rearranged to give the transformed signal by first listing the sums and then the differences. For example, if the input signal is:

5, 2, 3, 2, 5, 7, 9, 6
Then the sum and difference signals are:

s(i) = 7, 5, 12, 15

d(i) = 3, 1, -2, 3
Thus, the transformed signal is:

7, 5, 12, 15, 3, 1, -2, 3

The same process is applied recursively to the first half of the transformed signal, treating s(i)as the input signal, until the length of the input signal is 1. In the example above, the final transformed signal is:

39, -15, 2, -3, 3, 1, -2, 3
It is assumed that the length of the original input is a power of 2, and the input signal consists of integers between 0 and 255 (inclusive) only.

说明


倒推即可。不过需要多想一想。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,j,size;
    int a[260],b[520];
    while (scanf("%d", &n) && n){
        for (i = 1; i <= n; i++)
            scanf("%d", &a[i]);

        size = 2;

        while (size <= n){
            for (i = 1, j = 1; i <= size; i++, j += 2){
                b[j] = (a[i] + a[i + size / 2]) / 2;
                b[j+1] = (a[i] - a[i + size / 2]) / 2;
            }
            for (i = 1; i <= size; i++)
                a[i] = b[i];
            size *= 2;
        }

        for (i = 1; i < n; i++)
            printf("%d ", a[i]);
        printf("%d\n", a[n]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值